NotificationManager与NotificationManagerService交互流程

NotificationManager与NotificationManagerService交互流程

获取NotificationManager

  1. SDK提供API
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  1. 接着调用ContextImpl
class ContextImpl extends Context {
    @Override
    public Object getSystemService(String name) {
        return SystemServiceRegistry.getSystemService(this, name);
    }
}
  1. 接着调用SystemServiceRegistry
final class SystemServiceRegistry {
    static {
        //初始化 SYSTEM_SERVICE_FETCHERS
    }
    private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
            new HashMap<String, ServiceFetcher<?>>();
    public static Object getSystemService(ContextImpl ctx, String name) {
        ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
        return fetcher != null ? fetcher.getService(ctx) : null;
    }
}

NotificationManager#notify发送通知

public class NotificationManager {
    //最终调用到这里
    public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {
        1. INotificationManager service = getService();
        2. service.enqueueNotificationWithTag添加到NotificationManagerService通知队列里
    }
    public void cancelAsUser(String tag, int id, UserHandle user) {
        1. INotificationManager service = getService();
        2. service.cancelNotificationWithTag从NotificationManagerService通知队列里移除
    }
    //获取跨进程INotificationManager
    static public INotificationManager getService() {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService("notification");
        sService = INotificationManager.Stub.asInterface(b);
        return sService;
    }
}
    原文作者:lofiwang
    原文地址: https://blog.csdn.net/wcs542882916/article/details/83383043
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞