Android 通知用户更新或移除通知

来源:互联网 发布:php replace函数用法 编辑:程序博客网 时间:2024/06/01 09:26

原文地址:http://android.xsoftlab.net/training/notify-user/managing.html#Removing

当需要在不同时段发布同一事件类型的通知时,应当避免创建新的通知。相反的,应当考虑更新原有的通知,比如更改通知的某些值或者添加一些信息给通知。

下面的部分描述了如何更新通知以及如何移除通知。

修改通知

为了设置通知是可以更新的,需要在发布通知时由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 updatedint 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()方法,将已经发布的所有通知移除。

1 0
原创粉丝点击