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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章