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;
    }
}
发布了45 篇原创文章 · 获赞 8 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览