NotificationManagerService啓動流程以及和app交互實現原理

  1. SystemServer啓動(main方法被調用)
public final class SystemServer {
    public static void main(String[] args) {
        new SystemServer().run();
    }
    private void run() {
        mSystemServiceManager = new SystemServiceManager(mSystemContext);
        mSystemServiceManager.startService(NotificationManagerService.class);
    }
}
  1. NotificationManagerService啓動(onStart方法被調用)
public class SystemServiceManager {
    public SystemService startService(String className) {
        1. 反射創建NotificationManagerService對象
        2. 調用NotificationManagerService#onStart()方法
    }
}
  1. NotificationManagerService#onStart()初始化
public class NotificationManagerService extends SystemService {
    private final IBinder mService = new INotificationManager.Stub() {}
    @Override
    public void onStart() {
         1. 公佈mService,添加到ServiceManager
    }
}
  1. NotificationManager#getService()拿到INotificationManager對象
public class NotificationManager {
    private static INotificationManager sService
    static public INotificationManager getService() {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService("notification");
        sService = INotificationManager.Stub.asInterface(b);
        return sService;
    }
}
  1. NotificationManager通過INotificationManager接口調用NotificationManagerService裏的INotificationManager.Stub()對象
public class NotificationManagerService extends SystemService {
    private final IBinder mService = new INotificationManager.Stub() {
         1. 取消通知
         2. 獲取當前通知
         3. 註冊監聽器NotificationListenerService#INotificationListener
         3. 取消監聽器NotificationListenerService#INotificationListener
    }
}
  1. NotificationListenerService 裏封裝了INotificationListener對象
NotificationListenerService extends Service {
    private INotificationListenerWrapper mWrapper = null;
    private class INotificationListenerWrapper extends INotificationListener.Stub {}
    1. 創建INotificationListenerWrapper對象
    2. 通過INotificationManager註冊INotificationListener
}
  1. 實現INotificationListener回調接口
oneway interface INotificationListener
{
    void onListenerConnected(in NotificationRankingUpdate update);
    void onNotificationPosted(in IStatusBarNotificationHolder notificationHolder,
            in NotificationRankingUpdate update);
    void onNotificationRemoved(in IStatusBarNotificationHolder notificationHolder,
            in NotificationRankingUpdate update);
    void onNotificationRankingUpdate(in NotificationRankingUpdate update);
    void onListenerHintsChanged(int hints);
    void onInterruptionFilterChanged(int interruptionFilter);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章