全面解析Notification文档格式.docx

上传人:b****6 文档编号:19548857 上传时间:2023-01-07 格式:DOCX 页数:21 大小:26.01KB
下载 相关 举报
全面解析Notification文档格式.docx_第1页
第1页 / 共21页
全面解析Notification文档格式.docx_第2页
第2页 / 共21页
全面解析Notification文档格式.docx_第3页
第3页 / 共21页
全面解析Notification文档格式.docx_第4页
第4页 / 共21页
全面解析Notification文档格式.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

全面解析Notification文档格式.docx

《全面解析Notification文档格式.docx》由会员分享,可在线阅读,更多相关《全面解析Notification文档格式.docx(21页珍藏版)》请在冰豆网上搜索。

全面解析Notification文档格式.docx

//Notification.DEFAULT_ALLNotification.DEFAULT_SOUND添加声音//requiresVIBRATEpermission

.setSmallIcon(R.drawable.ic_launcher);

//设置通知小ICON

4.使用Builder创建通知

Notificationnotification=mBuilder.build();

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

intid=199;

LogUtils.d(TAG,"

创建通知"

);

mNotificationManager.notify(id,notification);

Notification重要方法解析

Notification的基本操作主要有创建、更新、取消这三种。

一个Notification的必要属性有三项,如果不设置则在运行时会抛出异常:

小图标,通过setSmallIcon()方法设置

标题,通过setContentTitle()方法设置

内容,通过setContentText()方法设置

除了以上三项,其它均为可选项。

虽然如此,但还是应该给Notification设置一个Action,这样就可以直接跳转到App的某个Activity、启动一个Service或者发送一个Broadcast。

否则,Notification仅仅只能起到通知的效果,而不能与用户交互。

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

1)setSmallIcon()与setLargeIcon()

在NotificationCompat.Builder中有设置通知的大小图标的两个方法。

这两个方法有什么区别呢?

当setSmallIcon()与setLargeIcon()同时存在时,smallIcon显示在largeIcon的右下角;

当只设置setSmallIcon()时,smallIcon显示在左侧。

看下图你就明白了。

对于部分ROM,可能修改过源码,如MIUI上通知的大图标和小图标是没有区别的。

这里写图片描述

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

Setthesmalliconresource,whichwillbeusedtorepresentthenotificationinthestatusbar.Theplatformtemplatefortheexpandedviewwilldrawthisiconintheleft,unlessalargeiconhasalsobeenspecified,inwhichcasethesmalliconwillbemovedtotheright-handside.

2)设置提醒标志符Flags

方法解释:

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

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

1.Notificationnotification=mBuilder.build();

2.notification.flags=Notification.FLAG_AUTO_CANCEL;

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

1.publicPendingIntentgetDefalutIntent(intflags){

2.PendingIntentpendingIntent=PendingIntent.getActivity(this,1,newIntent(),flags);

3.returnpendingIntent;

各标志符介绍

Notification.FLAG_SHOW_LIGHTS//三色灯提醒,在使用三色灯提醒时候必须加该标志符

Notification.FLAG_ONGOING_EVENT//发起正在运行事件(活动中)

Notification.FLAG_INSISTENT//让声音、振动无限循环,直到用户响应(取消或者打开)

Notification.FLAG_ONLY_ALERT_ONCE//发起Notification后,铃声和震动均只执行一次

Notification.FLAG_AUTO_CANCEL//用户单击通知后自动消失

Notification.FLAG_NO_CLEAR//只有全部清除时,Notification才会清除,不清楚该通知(QQ的通知无法清除,就是用的这个)

Notification.FLAG_FOREGROUND_SERVICE//表示正在运行的服务

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

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

对应属性:

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

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

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

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

/**

*显示带有默认铃声、震动、呼吸灯效果的通知

*如需实现自定义效果,请参考后面三个例子

*/

privatevoidshowNotifyWithMixed(){

NotificationCompat.Builderbuilder=newNotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher)

.setContentTitle("

我是有铃声+震动+呼吸灯效果的通知"

库里就是叼~"

//等价于setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);

.setDefaults(Notification.DEFAULT_ALL);

mManager.notify(5,builder.build());

4)setVibrate(long[]pattern)

设置震动的时间

.setVibrate(newlong[]{0,300,500,700});

实现效果:

延迟0ms,然后振动300ms,在延迟500ms,接着在振动700ms。

还有另外一种写法:

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

如果希望设置默认振动方式,设置了方法

(2)中默认为DEFAULT_VIBRATE即可。

例子:

*展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限

*<

uses-permissionandroid:

name="

android.permission.VIBRATE"

/>

*补充:

测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的

privatevoidshowNotifyWithVibrate(){

//震动也有两种设置方法,与设置铃声一样,在此不再赘述

long[]vibrate=newlong[]{0,500,1000,1500};

我是伴有震动效果的通知"

颤抖吧,凡人~"

//使用系统默认的震动参数,会与自定义的冲突

//.setDefaults(Notification.DEFAULT_VIBRATE)

//自定义震动效果

.setVibrate(vibrate);

//另一种设置震动的方法

//Notificationnotify=builder.build();

//调用系统默认震动

//notify.defaults=Notification.DEFAULT_VIBRATE;

//调用自己设置的震动

//notify.vibrate=vibrate;

//mManager.notify(3,notify);

mManager.notify(3,builder.build());

4)方法:

.setLights(intledARGB,intledOnMS,intledOffMS)

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

描述:

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

注意:

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

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

Notificationnotify=mBuilder.build();

notify.setLights(0xff00eeff,500,200)

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

notify.flags=Notification.FLAG_SHOW_LIGHTS;

notify.ledARGB=0xff00eeff;

notify.ledOnMS=500;

notify.ledOffMS=400;

如果希望使用默认的三色灯提醒,设置了方法

(2)中默认为DEFAULT_LIGHTS即可。

*显示带有呼吸灯效果的通知,但是不知道为什么,自己这里测试没成功

privatevoidshowNotifyWithLights(){

finalNotificationCompat.Builderbuilder=newNotificationCompat.Builder(this)

我是带有呼吸灯效果的通知"

一闪一闪亮晶晶~"

//ledARGB表示灯光颜色、ledOnMS亮持续时间、ledOffMS暗的时间

.setLights(0xFF0000,3000,3000);

Notificationnotify=builder.build();

//只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。

notify.flags=Notification.FLAG_SHOW_LIGHTS;

//设置lights参数的另一种方式

//notify.ledARGB=0xFF0000;

//notify.ledOnMS=500;

//notify.ledOffMS=5000;

//使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着

Handlerhandler=newHandler();

handler.postDelayed(newRunnable(){

@Override

publicvoidrun(){

mManager.notify(4,builder.build());

}

},10000);

}

5)方法:

.setSound(Urisound)

设置默认或则自定义的铃声,来提醒。

//获取默认铃声

.setDefaults(Notification.DEFAULT_SOUND)

//获取自定义铃声

.setSound(Uri.parse("

file:

///sdcard/dance.mp3"

))

//获取Android多媒体库内的铃声

.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"

5"

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

*展示有自定义铃声效果的通知

使用系统自带的铃声效果:

Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"

6"

privatevoidshowNotifyWithRing(){

我是伴有铃声效果的通知"

美妙么?

安静听~"

//调用系统默认响铃,设置此属性后setSound()会无效

//.setDefaults(Notification.DEFAULT_SOUND)

//调用系统多媒体裤内的铃声

//.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"

2"

));

//调用自己提供的铃声,位于/res/values/raw目录下

.setSound(Uri.parse("

android.resource:

//com.littlejie.notification/"

+R.raw.sound));

//另一种设置铃声的方法

//调用系统默认铃声

//notify.defaults=Notification.DEFAULT_SOUND;

//调用自己提供的铃声

//notify.sound=Uri.parse("

+R.raw.sound);

//调用系统自带的铃声

//notify.sound=Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"

//mManager.notify(2,notify);

mManager.notify(2,builder.build());

6)方法:

.setPriority(intpri)

设置优先级(实际项目中并无大用,设置最高级也不会使得你的通知栏出现在第一位)

Notification.PRIORITY_DEFAULT(优先级为0)

Notification.PRIORITY_HIGH

Notification.PRIORITY_LOW

Notification.PRIORITY_MAX(优先级为2)

Notification.PRIORITY_MIN(优先级为-2)

Notification.PRIORITY_MAX是优先级最高,Notification.PRIORITY_MIN优先级最低

7)方法:

setOngoing(booleanongoing)

设置为ture,表示它为一个正在进行的通知。

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

PS:

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

8)setProgress(intmax,intprogress,booleanindeterminate)

属性:

max:

进度条最大数值、progress:

当前进度、indeterminate:

表示进度是否不确定,true为不确定,false为确定

功能:

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

此方法在4.0及以后版本才有用,如果为早期版本:

需要自定义通知布局,其中包含ProgressBar视图

使用:

如果为确定的进度条:

调用setProgress(max,progress,false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调用setProgress(0,0,false)既可。

如果为不确定(持续活动)的进度条,这是在处理进度无法准确获知时显示活动正在持续,所以调用setProgress(0,0,true),操作结束时,调用setProgress(0,0,false)并更新通知以移除指示条

9)如何更新Notification

更新通知很简单,只需要再次发送相同ID的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;

如果之前的通知已经被取消,则会重新创建一个新通知。

更新通知跟发送通知使用相同的方式。

privatevoidrefreshNotification(){

//获取NotificationManager实例

NotificationManagernotifyManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

//实例化NotificationCmpat.Builde并设置相关属性

//设置小图标

.setSmallIcon(R.mipmap.icon_fab_repair)

//设置通知标题

最简单的Notification"

//设置通知内容

只有小图标、标题、内容"

//设置通知时间,默认为系统发出通知的时间,通常不用设置

//.setWhen(System.currentTimeMillis());

//通过builder.build()方法生成Notification对象,并发送通知,id=1

notifyManager.notify(1,builder.build());

10)如何取消Notification?

取消通知有如下5种方式:

1.点击通知栏的清除按钮,会清除所有可清除的通知

2.设置了setAutoCancel()或FLAG_AUTO_CANCEL的通知,点击该通知时会清除它

3.通过NotificationManager调用cancel(intid)方法清除指定ID的通知

4.通过NotificationManager调用cancel(Stringtag,intid)方法清除指定TAG和ID的通知

5.通过NotificationManager调用cancelAll()方法清除所有该应用之前发送的通知

如果你是通过NotificationManager.notify(Stringtag,intid,Notificationnotify)方法创建的通知,那么只能通过NotificationManager.cancel(Stringtag,intid)方法才能清除对应的通知

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1