1、相关代码:
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\StatusBar.java
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\NotificationListenerWithPlugins.java
frameworks\base\core\java\android\service\notification\NotificationListenerService.java
frameworks\base\core\java\android\app\Notification.java
frameworks\base\core\java\android\app\NotificationManager.java
frameworks\base\services\core\java\com\android\server\notification\NotificationManagerService.java
2、通知栏显示示例代码如下:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("thhtest",
"com.thh.test", NotificationManager.IMPORTANCE_LOW);
channel.enableLights(true);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
Notification noti = new Notification.Builder(getApplicationContext())
.setContentTitle("测试通知栏")
.setContentText("测试通知栏内容")
.setSmallIcon(R.mipmap.ic_launcher)
.setChannelId("thhtest")
.build();
notificationManager.notify(1001,noti);
以上就是最为简单的通知栏使用方式,显示效果仅仅是一个标题一个信息,均不超过一行,其余都是使用系统默认。
3、Notification分析:
Notification作为主要通知信息的载体,包含了所有通知信息的设置,如标题、信息、图标、点击后的intent、删除是的intent、提示音、led灯、自定义的view、大字体风格、添加按钮等等:
//通知栏点击时候触发的intent
public PendingIntent contentIntent;
//通知栏删除时候触发的intent
public PendingIntent deleteIntent;
//设置该intent,则效果类似短信提醒,会先在屏幕顶部弹出个小banner显示通知栏
public PendingIntent fullScreenIntent;
//6.0开始已经不再显示了
public CharSequence tickerText;
//6.0开始已经不再显示了
public RemoteViews tickerView;
//自定义显示的通知栏
public RemoteViews contentView;
//大版本的自定义通知栏,可以允许显示更多信息
public RemoteViews bigContentView;
//多媒体版本的自定义通知栏,可以添加一些案件
public RemoteViews headsUpContentView;
//大的图片用来显示在通知栏区域
public Bitmap largeIcon;
//通知提示音
public Uri sound;
//给通知栏添加的按钮,Notification.Builder#addAction(Notification.Action)
public static class Action
//通知栏信息载体,6.0后主要使用该方式:
* Notification noti = new Notification.Builder(mContext)
* .setContentTitle("New mail from " + sender.toString())
* .setContentText(subject)
* .setSmallIcon(R.drawable.new_mail)
* .setLargeIcon(aBitmap)
* .build();
public static class Builder
//大图片style:
* Notification notif = new Notification.Builder(mContext)
* .setContentTitle("New photo from " + sender.toString())
* .setContentText(subject)
* .setSmallIcon(R.drawable.new_post)
* .setLargeIcon(aBitmap)
* .setStyle(new Notification.BigPictureStyle()
* .bigPicture(aBigBitmap))
* .build();
public static class BigPictureStyle
//大文本style,可以显示下更多的内容
* Notification notif = new Notification.Builder(mContext)
* .setContentTitle("New mail from " + sender.toString())
* .setContentText(subject)
* .setSmallIcon(R.drawable.new_mail)
* .setLargeIcon(aBitmap)
* .setStyle(new Notification.BigTextStyle()
* .bigText(aVeryLongString))
* .build();
public static class BigTextStyle
//信息排列模式,将显示信息一条条排列:
* Notification noti = new Notification.Builder()
* .setContentTitle("2 new messages wtih " + sender.toString())
* .setContentText(subject)
* .setSmallIcon(R.drawable.new_message)
* .setLargeIcon(aBitmap)
* .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
* .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
* .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
* .build();
public static class MessagingStyle
4、通知栏显示流程分析
4.1 NotificationManager
调用nofity之后,进入notifyAsUser,如下:
public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
{
int[] idOut = new int[1];
INotificationManager service = getService();
String pkg = mContext.getPackageName();
//补充notification的EXTRA_BUILDER_APPLICATION_INFO和EXTRA_ORIGINATING_USERID信息
Notification.addFieldsFromContext(mContext, notification);
//校验提示音文件可行性
if (notification.sound != null) {
notification.sound = notification.sound.getCanonicalUri();
if (StrictMode.vmFileUriExposureEnabled()) {
notification.sound.checkFileUriExposed("Notification.sound");
}
}
//修复smallIcon设置
fixLegacySmallIcon(notification, pkg);
//5.0后没有设置smallIcon的不让显示
if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
if (notification.getSmallIcon() == null) {
throw new IllegalArgumentException("Invalid notification (no valid small icon): "
+ notification);
}
}
//如果设置了sty