Android:仅在触摸时删除通知,而不是通过“全部清除";按钮 [英] Android: Delete Notification only when touched not throuch "clear all" Button

查看:79
本文介绍了Android:仅在触摸时删除通知,而不是通过“全部清除";按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序会创建一些通知,在 PendingIntents 的帮助下,它会将您带到一个特殊的 Activity.一切都很好,我唯一的问题是我希望通知仅在用户触摸时删除,而不是在用户使用全部清除"按钮(通知栏顶部的 X)时删除.

my App creates a few notifications and with the Help of PendingIntents it takes you to an special Activity. It all works pretty fine, the only problem i have is that i want the Notification to only be deleted when the user touches it, not when the user uses the "Clear all" Button ( X on top of the Notificationbar).

目前我正在使用

setAutoCancel(true);

方法.当用户触摸它时,它会删除通知,而且在按下清除所有"按钮时也会删除它.我的问题是现在是否有任何方法可以抑制被全部清除"按钮删除的通知.

method. It deletes the Notification when the user touches it, but also when the "Clear ALl" Button is pressed. My Question is now if theres any way i can surpress the Notification being deleted by the "Clear All" Button.

如果你想知道我为什么想要这个,我创建了一个应用程序,它允许你使用通知作为一种提醒.这里的清除按钮"很烦人,因为它会删除所有提醒通知.

If you wonder why i want this, i created a App thats allows you to use Notifications as an kind of reminder. The "Clear Button" is annoying here then, because it deletes all the reminder notifications.

感谢帮助,如果您需要任何其他代码,只需编写即可.

Thx for the Help, if you want any additional Code, just write it.

推荐答案

您可以通过将通知设置为进行中,然后在 PendingIntent 触发时取消通知来实现此目的.

You can accomplish this by setting your Notification as ongoing, and then cancelling it when the PendingIntent fires.

用户无法关闭正在进行的通知,因此您的应用程序或服务必须注意取消它们.

Ongoing notifications cannot be dismissed by the user, so your application or service must take care of canceling them.

假设您使用 Notification.Builder 来创建通知,只需在调用 build() 之前添加以下行.

Assuming you're using a Notification.Builder to create your Notification, simply add the following line before calling build().

.setOngoing(true)

如果您希望删除通知,请使用发布通知时使用的相同 ID 在 NotificationManager 服务上调用 cancel().

When you wish to remove the Notification, call cancel() on the NotificationManager Service with the same ID used when posting the Notification.

这篇关于Android:仅在触摸时删除通知,而不是通过“全部清除";按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆