全面解析Notification

Notification在Android中使用的频率可以说是非常高的,本文我将围绕着Notification的各方面进行解析,使大家对Notification有更好的认识。

成都创新互联公司专注于龙里企业网站建设,响应式网站建设,商城网站建设。龙里网站建设公司,为龙里等地区提供建站服务。全流程按需网站设计,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

Notification的使用步骤

1.获取NotificationManager

 
 
 
 
  1. NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2.创建NotificationCompat.Builder

 
 
 
 
  1. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

3.对Builder设置一些Notification相关属性:

 
 
 
 
  1. mBuilder.setContentTitle("标题")//设置通知栏标题  
  2.     .setContentText("内容") //设置通知栏显示内容 
  3.     .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  
  4. //  .setNumber(number) //设置通知集合的数量  
  5.     .setTicker("通知到来") //通知***出现在通知栏,带上升动画效果的  
  6.     .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间  
  7.     .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  
  8. //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消    
  9.     .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)  
  10.     .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合  
  11.     //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission  
  12.     .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON  

4.使用Builder创建通知

 
 
 
 
  1. Notification notification = mBuilder.build();

5.使用NotificationManager将通知推送出去

 
 
 
 
  1. int id = 199;
  2. LogUtils.d(TAG, "创建通知");
  3. mNotificationManager.notify(id, notification); 

Notification重要方法解析

Notification 的基本操作主要有创建、更新、取消这三种。一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:

  1. 小图标,通过 setSmallIcon() 方法设置
  2. 标题,通过 setContentTitle() 方法设置
  3. 内容,通过 setContentText() 方法设置

除了以上三项,其它均为可选项。虽然如此,但还是应该给 Notification 设置一个 Action ,这样就可以直接跳转到 App 的某个 Activity 、启动一个 Service 或者发送一个 Broadcast。否则,Notification 仅仅只能起到通知的效果,而不能与用户交互。

当系统接收到通知时,可以通过震动、响铃、呼吸灯等多种方式进行提醒。

1) setSmallIcon() 与 setLargeIcon()

在 NotificationCompat.Builder 中有设置通知的大小图标的两个方法。这两个方法有什么区别呢?当 setSmallIcon() 与 setLargeIcon() 同时存在时, smallIcon 显示在largeIcon的右下角;当只设置 setSmallIcon() 时, smallIcon 显示在左侧。看下图你就明白了。对于部分 ROM ,可能修改过源码,如 MIUI 上通知的大图标和小图标是没有区别的。

Google 官方是这么解释 setSmallIcon() 这个方法的:

 
 
 
 
  1. Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.

2) 设置提醒标志符Flags

方法解释:提醒标志符,向通知添加声音、闪灯和振动效果等设置达到通知提醒效果,可以组合多个属性

a) 创建通知栏之后通过给他添加.flags属性赋值。

 
 
 
 
  1. Notification notification = mBuilder.build();  
  2. notification.flags = Notification.FLAG_AUTO_CANCEL;  

b) 通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags

 
 
 
 
  1. public PendingIntent getDefalutIntent(int flags){
  2. PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
  3. return pendingIntent;

各标志符介绍

 
 
 
 
  1. Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符
  2. Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)
  3. Notification.FLAG_INSISTENT   //让声音、振动***循环,直到用户响应 (取消或者打开)
  4. Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次
  5. Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失
  6. Notification.FLAG_NO_CLEAR          //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
  7. Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务 

3) .setDefaults(int defaults)(NotificationCompat.Builder中的方法,用于设置通知到来时,通过什么方式进行提示)

方法解释:向通知添加声音、闪灯和振动效果的最简单、使用默认(defaults)属性,可以组合多个属性(和方法1中提示效果一样的)

对应属性:

Notification.DEFAULT_VIBRATE //添加默认震动提醒 需要 VIBRATE permission

Notification.DEFAULT_SOUND // 添加默认声音提醒

Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒

Notification.DEFAULT_ALL// 添加默认以上3种全部提醒

 
 
 
 
  1. /**
  2. * 显示带有默认铃声、震动、呼吸灯效果的通知
  3. * 如需实现自定义效果,请参考后面三个例子
  4. */
  5. private void showNotifyWithMixed() {
  6.    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  7.            .setSmallIcon(R.mipmap.ic_launcher)
  8.            .setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
  9.            .setContentText("库里就是叼~")
  10.            //等价于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
  11.            .setDefaults(Notification.DEFAULT_ALL);
  12.    mManager.notify(5, builder.build());

4) setVibrate(long[] pattern)

方法解释:设置震动的时间

 
 
 
 
  1. .setVibrate(new long[] {0,300,500,700});  

实现效果:延迟0ms,然后振动300ms,在延迟500ms,接着在振动700ms。

还有另外一种写法:

 
 
 
 
  1. mBuilder.build().vibrate = new long[] {0,300,500,700};  

如果希望设置默认振动方式,设置了方法(2)中默认为DEFAULT_VIBRATE 即可。

例子:

 
 
 
 
  1. /**
  2. * 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
  3. * 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
  4. */
  5. private void showNotifyWithVibrate() {
  6.    //震动也有两种设置方法,与设置铃声一样,在此不再赘述
  7.    long[] vibrate = new long[]{0, 500, 1000, 1500};
  8.    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  9.            .setSmallIcon(R.mipmap.ic_launcher)
  10.            .setContentTitle("我是伴有震动效果的通知")
  11.            .setContentText("颤抖吧,凡人~")
  12.            //使用系统默认的震动参数,会与自定义的冲突
  13.            //.setDefaults(Notification.DEFAULT_VIBRATE)
  14.            //自定义震动效果
  15.            .setVibrate(vibrate);
  16.    //另一种设置震动的方法
  17.    //Notification notify = builder.build();
  18.    //调用系统默认震动
  19.    //notify.defaults = Notification.DEFAULT_VIBRATE;
  20.    //调用自己设置的震动
  21.    //notify.vibrate = vibrate;
  22.    //mManager.notify(3,notify);
  23.    mManager.notify(3, builder.build());

4)方法:.setLights(intledARGB ,intledOnMS ,intledOffMS )

方法解释:android支持三色灯提醒,这个方法就是设置不同场景下的不同颜色的灯。

描述:其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。

注意:

1)只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持三色灯提醒。

2)这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。

 
 
 
 
  1. Notification notify = mBuilder.build();  
  2. notify .setLights(0xff00eeff, 500, 200) 

同理,以下方法也可以设置同样效果:

 
 
 
 
  1. Notification notify = mBuilder.build();  
  2. notify.flags = Notification.FLAG_SHOW_LIGHTS;  
  3. notify.ledARGB = 0xff00eeff;  
  4. notify.ledOnMS = 500;  
  5. notify.ledOffMS = 400;  

如果希望使用默认的三色灯提醒,设置了方法(2)中默认为DEFAULT_LIGHTS即可。

例子:

 
 
 
 
  1. /**
  2. * 显示带有呼吸灯效果的通知,但是不知道为什么,自己这里测试没成功
  3. */
  4. private void showNotifyWithLights() {
  5.    final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  6.            .setSmallIcon(R.mipmap.ic_launcher)
  7.            .setContentTitle("我是带有呼吸灯效果的通知")
  8.            .setContentText("一闪一闪亮晶晶~")
  9.            //ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
  10.            .setLights(0xFF0000, 3000, 3000);
  11.    Notification notify = builder.build();
  12.    //只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
  13.    notify.flags = Notification.FLAG_SHOW_LIGHTS;
  14.    //设置lights参数的另一种方式
  15.    //notify.ledARGB = 0xFF0000;
  16.    //notify.ledOnMS = 500;
  17.    //notify.ledOffMS = 5000;
  18.    //使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着
  19.    Handler handler = new Handler();
  20.    handler.postDelayed(new Runnable() {
  21.        @Override
  22.        public void run() {
  23.            mManager.notify(4, builder.build());
  24.        }
  25.    }, 10000);

5)方法:.setSound(Uri sound)

方法解释:设置默认或则自定义的铃声,来提醒。

 
 
 
 
  1. //获取默认铃声  
  2. .setDefaults(Notification.DEFAULT_SOUND)  
  3. //获取自定义铃声  
  4. .setSound(Uri.parse("file:///sdcard/dance.mp3"))  
  5. //获取Android多媒体库内的铃声  
  6. .setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"))   

同理相同效果的另一种设置方法这边就不讲, 和上面的都是一样的。

例子:

 
 
 
 
  1. /**
  2. * 展示有自定义铃声效果的通知
  3. * 补充:使用系统自带的铃声效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
  4. */
  5. private void showNotifyWithRing() {
  6.    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
  7.            .setSmallIcon(R.mipmap.ic_launcher)
  8.            .setContentTitle("我是伴有铃声效果的通知")
  9.            .setContentText("美妙么?安静听~")
  10.            //调用系统默认响铃,设置此属性后setSound()会无效
  11.            //.setDefaults(Notification.DEFAULT_SOUND)
  12.            //调用系统多媒体裤内的铃声
  13.            //.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));
  14.            //调用自己提供的铃声,位于 /res/values/raw 目录下
  15.            .setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));
  16.    //另一种设置铃声的方法
  17.    //Notification notify = builder.build();
  18.    //调用系统默认铃声
  19.    //notify.defaults = Notification.DEFAULT_SOUND;
  20.    //调用自己提供的铃声
  21.    //notify.sound = Uri.parse("android.resource://com.littlejie.notification/"+R.raw.sound);
  22.    //调用系统自带的铃声
  23.    //notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2");
  24.    //mManager.notify(2,notify);
  25.    mManager.notify(2, builder.build());

6)方法:.setPriority(int pri)

方法解释:设置优先级(实际项目中并无大用,设置***级也不会使得你的通知栏出现在***位)

对应属性:

  1. Notification.PRIORITY_DEFAULT(优先级为0)
  2. Notification.PRIORITY_HIGH
  3. Notification.PRIORITY_LOW
  4. Notification.PRIORITY_MAX(优先级为2)
  5. Notification.PRIORITY_MIN(优先级为-2)

Notification.PRIORITY_MAX是优先级***,Notification.PRIORITY_MIN优先级***

7)方法:setOngoing(boolean ongoing)

方法解释:设置为ture,表示它为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)

PS:我们看到360手机卫士的通知栏一直固定在手机中,就是通过设置这个标记,使用该标记后你的通知栏无法被用户手动进行删除,只能通过代码进行删除,慎用

8)setProgress(int max, int progress,boolean indeterminate)

属性:max:进度条***数值 、progress:当前进度、indeterminate:表示进度是否不确定,true为不确定,false为确定

功能:设置带进度条的通知,可以在下载中使用

注意:此方法在4.0及以后版本才有用,如果为早期版本:需要自定义通知布局,

当前文章:全面解析Notification
标题网址:http://www.gawzjz.com/qtweb/news9/198809.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联