本编文章针对AndroidO 8.0代码讲述。此版本的SystemUI和Notification较之前版本有一些改动。
本编文章讲述展示过程: System Notification --> SystemUI --> Display Notifications。
第一编文章《Android O 8.0 Notification 源码分析(一)》讲述生成过程:
App create Notification ----> System's NotificationManagerService。
此便文章的目的为方便自己回忆和给需要的同学参考下。
还是先把整体流程图贴出来:
咱们直接从StatusBar的start()方法开始,到底SystemUI怎么重启的流程,咱们下回分解。
步骤1,2,3,4,5,5,6,7,8,10,11,13:为SystemUI的Statusbar,Navigationbar,Notificationpanel自定义控件的初始化。
步骤15:初始化NotificationListenerWithPlugins,并且注册:
public class NotificationListenerWithPlugins extends NotificationListenerService implements
PluginListener<NotificationListenerController> {
//下面只列出了类中方法名,具体实现自己翻代码。
registerAsSystemService();
unregisterAsSystemService();
getActiveNotifications();
getCurrentRanking();
...
}
下面在start()方法注册service:
// Set up the initial notification state.
try {
mNotificationListener.registerAsSystemService(mContext,
new ComponentName(mContext.getPackageName(), getClass().getCanonicalName()),
UserHandle.USER_ALL);
} catch (RemoteException e) {
Log.e(TAG, "Unable to register notification listener", e);
}
下面代码为在destroy()方法中注销service:
public void destroy() {
// Begin old BaseStatusBar.destroy().
mContext.unregisterReceiver(mBaseBroadcastReceiver);
try {
mNotificationListener.unregisterAsSystemService();
} catch (RemoteException e) {
// Ignore.
}
}
步骤9,12,14,16,18:为listener的回调方法会调用
步骤17,19 StatusBar的addNotification(),updateNotification()方法;
public void addNotification(StatusBarNotification notification, RankingMap ranking)
throws InflationException {
String key = notification.getKey();
if (DEBUG) Log.d(TAG, "addNotification key=" + key);
mNotificationData.updateRanking(ranking);
Entry shadeEntry = createNotificationViews(notification);
...
//提示方式
}
步骤20,21:
protected NotificationData.Entry createNotificationViews(StatusBarNotification sbn)
throws InflationException {
NotificationData.Entry entry = new NotificationData.Entry(sbn);
Dependency.get(LeakDetector.class).trackInstance(entry);
entry.createIcons(mContext, sbn);
// Construct the expanded view.
i