1、在应用中创建Notification 这里是创建并发送一个通知,
Notification notification2 = null;
CharSequence aVeryLongString="hello";
// 如果当前Android的版本相比Android O,一样或者版本更高,就建通知渠道(Notification Channels )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
//1.0 建渠道
NotificationChannel mChannel = new NotificationChannel(id, name,
NotificationManager.IMPORTANCE_LOW);
//2.0 把通知渠道通过createNotificationChannel( )方法给-
// -状态栏通知的管理类 NotificationManager
manager.createNotificationChannel(mChannel);
//3.0 Notification这时候可以正常工作了
CharSequence cs="hello";
NotificationCompat.BigTextStyle bs = new NotificationCompat.BigTextStyle().bigText(cs);
notification2 = new NotificationCompat.Builder(this, id)
.setStyle(bs)
.setContentTitle("这是一个内容标题2")//设置通知栏标题
.setContentText("这是一个内容文本2") //设置通知栏显示内容
.setWhen(System.currentTimeMillis())//通知产生的时间。
.setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher))//设置通知大ICON
.build();
}
manager.notify(1, notification2);
2、systemUI 中显示通知
1.SystemUIFactory 创建NotificationListener,它是一个服务,用于plugin监听和通知的监听
2.如果有通知会onNotificationPosted 这里,分两类
1) mEntryManager.updateNotification(sbn, rankingMap);更通知
2) mEntryManager.addNotification(sbn, rankingMap);添加通知
3.SystemUI用 Dependency 创建ForegroundServiceControllerImpl对象,这是以个控制器,真正的使用是在
4.通知的管理以及布局的解析都是在NotificationEntryManager类中。(省略)
ExpandableNotificationRow具体通知的显示
NotificationShelf将通知变化成图标,在快捷面板拉伸时显示。
FooterView 显示“通知管理”和“全部清除”按钮
NotificationEntryManagerupdateNotification -> updateNotificationInternal() -> inflateViews()->updateNotification()
private void inflateViews(NotificationData.Entry entry, ViewGroup parent) {
PackageManager pmUser = StatusBar.getPackageManagerForUser(mContext,
entry.notification.getUser().getIdentifier());
final StatusBarNotification sbn = entry.notification;
if (entry.row != null) {
entry.reset();
updateNotification(entry, pmUser, sbn, entry.row);
} else {
new RowInflaterTask().inflate(mContext, parent, entry,
row -> {
bindRow(entry, pmUser, sbn, row);
updateNotification(entry, pmUser, sbn, row);
});
}
}
接下来会调到这里inflateNotificationViews
解析布局。最终会计算出显示的文本颜色。
public void updateNotification(NotificationData.Entry entry) {
mEntry = entry;
mStatusBarNotification = entry.notification;
mNotificationInflater.inflateNotificationViews();
cacheIsSystemNotification();
}
最终在frameworks/base/core/res/res/values/colors.xml配置通知文本显示的颜色。 可以在overlay中更改。
<color name="notification_primary_text_color_light">@color/primary_text_default_material_light</color> //大标题显示的颜色 内容
<color name="notification_primary_text_color_dark">@color/primary_text_default_material_dark</color>
<color name="notification_secondary_text_color_light">@color/primary_text_default_material_light</color>// 小文本显示的颜色 时间
<item name="notification_secondary_text_disabled_alpha" format="float" type="dimen">0.38</item>
<color name="notification_secondary_text_color_dark">@color/primary_text_default_material_dark</color>
<color name="notification_default_color_dark">@color/primary_text_default_material_light</color>
<color name="notification_default_color_light">#a3202124</color>
```