通知栏显示流程

本文详细解析了Android系统中通知栏显示的流程,从NotificationManager开始,经过NotificationManagerService的处理,包括通知的检查、完善、通道检查、发送等步骤,再到Notification的构造和功能,最后探讨了NotificationListenerService的注册与监听机制,阐述了系统如何回调并展示通知。
摘要由CSDN通过智能技术生成
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值