NotificationListenerService系統app用法

NotificationListenerService系統app用法無需用戶授權

  1. 創建對象NotificationListenerService並調用registerAsSystemService
public abstract class BaseStatusBar {
    private final NotificationListenerService mNotificationListener = new NotificationListenerService() {
        @Override
        public void onListenerConnected() {}
        @Override
        public void onNotificationPosted(...) {}
        @Override
        public void onNotificationRemoved(...) {}
        ......
    }
    mNotificationListener.registerAsSystemService(...);
    mNotificationListener.unregisterAsSystemService();
}
  1. registerAsSystemService過程

    INotificationManager註冊INotificationListenerWrapper,繼承至INotificationListener.Stub
public abstract class NotificationListenerService {
    public void registerAsSystemService(...) {
        mWrapper = new INotificationListenerWrapper();
        INotificationManager noMan = getNotificationInterface();
        noMan.registerListener(mWrapper, componentName, currentUser);
    }
    private class INotificationListenerWrapper extends INotificationListener.Stub {}

    protected final INotificationManager getNotificationInterface() {
        if (mNoMan == null) {
            mNoMan = INotificationManager.Stub.asInterface(
                    ServiceManager.getService(Context.NOTIFICATION_SERVICE));
        }
        return mNoMan;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章