通知栏Notification和常驻通知栏

通知栏Notification和常驻通知栏:

&&.普通Notification
一、整体步骤
//1.获取通知栏管理器实例
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
//2.获取要弹出的Notification
Notification notification = ....自己封装的构建类及相应方法....
//3.弹出Notification
manager.notify( 【该通知对应的ID值】, notification );


二、其中第二步生成Notification:
两类方法
1.使用系统默认样式
    参考默认样式的示意图,和各个参数以及构造方法的说明,设置各个字段的值.
     可以使用notification.setContentIntent()或者其它手段来设置对应的PendingIntent.
     注意:该方式下,不同api版本区间最实用的构造方法或许不同,可以参考相关资料。

2.自定义样式
    2.1自定义RemoteView样式,获取RemoteView
    2.2将notification与RemoteView绑定
        notification.contentView = remoteView;
        此时务必使用以下方式来指定notification的点击事件:
        notification.contentIntent = pendingIntent;

参考:


三、关于上面RemoteView的自定义:
1.可以为RemoteView指定对应的布局文件,来确定样式
2.可以为里面每个不同的View绑定不同的PendingIntent
(经我测试,没有指明PendingIntent的部分,点击会触发整个Notification绑定的PendingIntent)


&&.常驻通知栏
    只要设置一下notification.falgs的值就行了,与常量 Notification.FLAG_ONGOING_EVENT 进行“位或”运算就行了:
    notice.flags |= Notification.FLAG_ONGOING_EVENT;//该通知常驻在通知栏,始终存在

    其它类似的语句还有:
    notice.flags |= Notification.FLAG_AUTO_CANCEL;//点击后该通知消失

    顺便说一下Notificaiton.flags,
    在Notification类中定义了一系列二进制常量,因此上面是“|=”即“位或”运算,它们的对应的效果如下:(我没全部尝试)
    public static final int FLAG_SHOW_LIGHTS        = 0x00000001;//控制闪光

     public static final int FLAG_ONGOING_EVENT      = 0x00000002;//将flag设置为这个属性那么通知就会常驻

     public static final int FLAG_INSISTENT          = 0x00000004; //重复发出声音,直到用户响应此通知 

     public static final int FLAG_ONLY_ALERT_ONCE    = 0x00000008;//标记声音或者震动一次

     public static final int FLAG_AUTO_CANCEL        = 0x00000010; //在通知栏上点击此通知后自动清除此通知 

     public static final int FLAG_NO_CLEAR           = 0x00000020;//将flag设置为该值,则通知栏的清楚按钮就不会出现

     public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;//前台服务标记

     public static final int FLAG_HIGH_PRIORITY = 0x00000080;//高权限??? 

猜你喜欢

转载自blog.csdn.net/u013914309/article/details/76060061