Android 9.0系统源码_通知服务(二)系统状态栏是如何监听通知服务的各种通知事件的

文章详细阐述了Android9.0系统中,StatusBar如何通过NotificationListener监听并处理NotificationManagerService的通知事件。首先,StatusBar启动后调用NotificationListener的setUpWithPresenter方法,设置回调。接着,NotificationListener通过registerAsSystemService方法注册到系统服务,创建NotificationListenerWrapper作为Binder代理对象。最后,NotificationManagerService接收到注册请求后,通过ManagedServices管理监听器,并在连接成功时调用回调方法,将事件传递回StatusBar进行处理。

前言

上一篇文章简单讲述了系统通知服务NotificationManagerService的启动流程,本篇文章我们将会具体梳理一下SystemUI组件系统状态栏StatusBar是如何监听通知服务的各种通知事件的。

一、StatusBar调用NotificationListener的setUpWithPresenter

1、我们在Android 9.0系统源码_SystemUI(一)SystemUI的启动流程系列文章有具体分析过SystemUI的启动流程,在状态栏StatusBar被创建之后,首先触发的便是start方法。

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java

public class StatusBar extends SystemUI implements DemoMode,
        DragDownHelper.DragDownCallback, ActivityStarter, OnUnlockMethodChangedListener,
        OnHeadsUpChangedListener, CommandQueue.Callbacks, ZenModeController.Callback,
        ColorExtractor.OnColorsChangedListener, ConfigurationListener, NotificationPresenter {
   
   
    
    protected NotificationListener mNotificationListener;
    protected NotificationEntryManager mEntryManager;

    @Override
    public void start() {
   
   
        ...代码省略...
        mNotificationListener =  Dependency.get(NotificationListener.class);
        mEntryManager = Dependency.get(NotificationEntryManager.class);
    	...代码省略...
        createAndAddWindows();//创建状态栏并添加到窗口上
    	...代码省略...
        // Set up the initial notification state.
        //监听NotificationManagerService的通知消息
        mNotificationListener.setUpWithPresenter(this, mEntryManager);
	   	...代码省略...
    }

}

在start方法中会获取NotificationListener实例对象,并调用该对象的setUpWithPresenter方法。

二、NotificationListener设置通知服务的回调方法。

1、NotificationListener的setUpWithPresenter方法如下所示。

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java

public class NotificationListener extends NotificationListenerWithPlugins {
   
   

    private static final String TAG = "NotificationListener";
    private final Context mContext;
    protected NotificationPresenter mPresenter;
    protected NotificationEntryManager mEntryManager;

    public NotificationListener(Context context) {
   
   
        mContext = context;
    }

    @Override
    public void onListenerConnected() {
   
   
 		...连接成功...
    }

    @Override
    public void onNotificationPosted(final StatusBarNotification sbn,
            final RankingMap rankingMap) {
   
   
     	...新的通知...
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn,
            final RankingMap rankingMap) {
   
   
 		...移除通知...
    }

    @Override
    public void onNotificationRankingUpdate(final RankingMap rankingMap) {
   
   
 		...更新通知...
    }

    public void setUpWithPresenter(NotificationPresenter presenter, NotificationEntryManager entryManager) {
   
   
        mPresenter = presenter;
        mEntryManager = entryManager;
        try {
   
   
            registerAsSystemService(mContext, new ComponentName(mContext.getPackageName(), getClass().getCanonicalName()), UserHandle.USER_ALL);
        } catch (RemoteException e) {
   
   
            Log.e(TAG, "Unable to register notification listener", e);
        }
    }
}

NotificationListener自身的各种回调方法会在特定情况下被通知服务所触发,而设置监听通知服务的setUpWithPresenter方法在给mPresenter和mEntryManager赋值之后,会进一步调用父类NotificationListenerWithPlugins的registerAsSystemService方法。

2、NotificationListenerWithPlugins的registerAsSystemService方法如下所示。

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationListenerWithPlugins.java

public class NotificationListenerWithPlugins extends NotificationListenerService implements
        PluginListener<NotificationListenerController> {
   
   
    @Override
    public void registerAsSystemService(Context context, ComponentName componentName,
            int currentUser) throws RemoteException {
   
   
        super.register
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值