前言
上一篇文章简单讲述了系统通知服务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

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

被折叠的 条评论
为什么被折叠?



