从通知栏删除所有通知 [英] Remove all notifications from notification bar

查看:106
本文介绍了从通知栏删除所有通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从状态栏中删除所有可取消的通知.我知道如何删除我之前看到过此帖子的应用通知,但是我想要删除其他应用程序的通知.

I want to remove all cancelable notifications from status bar. I know how to remove my app notifications I have seen this post before, butI want to remove other apps notifications.

在所有android版本的通知中都有一个清除"按钮,可清除带有以下标志的所有通知:Notification.FLAG_AUTO_CANCEL

There is a "Clear" button in notifications in all android versions which clears all notifications with this flag: Notification.FLAG_AUTO_CANCEL

这意味着可以取消所有通知.但是我还没有找到任何有关如何执行此操作的文档.有没有人指导我该怎么做?

That means it's possible to cancel all notifications. But I haven't found any document about how to do that. Is there anybody guide me how to do that?

谢谢

推荐答案

从API级别18开始,您可以使用NotificationListenerService取消与您自己的应用程序不同的其他应用程序发布的通知,该方法在Lollipop中有所更改,这是删除还包含Lillipop API的通知的方法.

Starting with API Level 18 you can cancel Notifications posted by other apps different than your own using NotificationListenerService, the method changed a little in Lollipop, here is the way to remove notifications covering also Lillipop API.

首先在onNotificationPosted方法内部存储所有StatusBarNotification对象. 然后,您应该维护对这些对象的更新引用,如果在onNotificationRemoved方法中以某种方式取消了通知,则将其删除.

First inside the onNotificationPosted method you store all the StatusBarNotification objects. Then you should maintain an updated reference to those objects, removing them if the notification is somehow dismissed in onNotificationRemoved method.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // Store each StatusBarNotification object
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // Delete removed StatusBarNotification objects
    }
}

最后,您可以使用cancelNotification方法删除任何通知.

Finally you can remove any notification using cancelNotification method.

 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
    cancelNotification(sbn.getKey());
}

这篇关于从通知栏删除所有通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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