Android官方开发文档Training系列课程中文版:通知用户之更新或移除通知
2016-06-21 10:16
513 查看
原文地址:http://android.xsoftlab.net/training/notify-user/managing.html#Removing
当需要在不同时段发布同一事件类型的通知时,应当避免创建新的通知。相反的,应当考虑更新原有的通知,比如更改通知的某些值或者添加一些信息给通知。
下面的部分描述了如何更新通知以及如何移除通知。
下面的代码段演示了在事件发生时,一条通知将会被用来更新该事件的数目:
用户移除了该通知或者使用了”Clear All”功能(如果通知是可移除的话)。
用户点击了通知,这条通知在创建时使用了setAutoCancel(false)方法(false是默认属性)。
通过调用cancel()方法并指定该通知的ID。这个方法还可以移除进行中的通知。
通过调用cancelAll()方法,将已经发布的所有通知移除。
当需要在不同时段发布同一事件类型的通知时,应当避免创建新的通知。相反的,应当考虑更新原有的通知,比如更改通知的某些值或者添加一些信息给通知。
下面的部分描述了如何更新通知以及如何移除通知。
修改通知
为了设置通知是可以更新的,需要在发布通知时由NotificationManager.notify(ID, notification)方法指定该通知的ID。为了更新这条通知,需要更新或者创建一个NotificationCompat.Builder对象,并由这个对象构建一个Notification对象,然后将这个通知对象以相同的ID发布出去。下面的代码段演示了在事件发生时,一条通知将会被用来更新该事件的数目:
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Sets an ID for the notification, so it can be updated int notifyID = 1; mNotifyBuilder = new NotificationCompat.Builder(this) .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallIcon(R.drawable.ic_notify_status) numMessages = 0; // Start of a loop that processes data and then notifies the user ... mNotifyBuilder.setContentText(currentText) .setNumber(++numMessages); // Because the ID remains unchanged, the existing notification is // updated. mNotificationManager.notify( notifyID, mNotifyBuilder.build()); ...
移除通知
在以下事件发生时,通知将会从通知栏中移除:用户移除了该通知或者使用了”Clear All”功能(如果通知是可移除的话)。
用户点击了通知,这条通知在创建时使用了setAutoCancel(false)方法(false是默认属性)。
通过调用cancel()方法并指定该通知的ID。这个方法还可以移除进行中的通知。
通过调用cancelAll()方法,将已经发布的所有通知移除。
相关文章推荐
- Android开发:shape和selector和layer-list的(详细说明)
- Android:res之shape制作圆角、虚线、渐变
- ImagerLoader的原理
- 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
- Android 最简单的SD卡文件遍历程序
- android 数据存储的五大方式(转)
- android-R.drawable
- android-R.id
- android 遍历所有文件夹和子目录搜索文件
- Android (shape,gradient)使用总结
- Android中的SpannableString以及Pattern/Matcher的使用
- Android:FLAG_KEEP_SCREEN_ON
- 使用定时器实现简单的字体闪烁并实现字体跑马灯
- android-R.dimen
- Android消息循环机制
- Android Studio NDK JNI配置Demo超详细2016
- Android中自定义属性(attrs.xml,TypedArray的使用)
- android四大组件介绍
- android读取短信内容
- android-R.color