开发可用作DND应用程序并阻止给定时间的所有通知的应用程序

时间:2019-05-05 04:04:22

标签: android android-notifications android-notification-bar notificationmanager

我是android的初学者,我想开发一个可在一段时间内阻止所有其他应用程序通知的应用程序。是否可以阻止其他应用程序通知? 如果可能,有人可以为此指导我吗?有人可以帮助我吗?

我使用了一种方法,即InterruptionFilter的方法,但是不起作用。下面是我使用过的代码。

mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

//On button click calling the below method
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);

1 个答案:

答案 0 :(得分:0)

NotificationListenerService是最合适的选择。

一种服务,在发布或删除新通知或更改其排名时接收来自系统的呼叫。

要扩展此类,您必须在具有Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE权限的清单文件中声明服务,并在SERVICE_INTERFACE操作中包含意图过滤器。例如:

<service android:name=".NotificationListener"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
 </service>

文档:NotificationListnerService

可能您需要实现cancelAllNotifications()cancelNotification(String key)之类的方法来阻止其他应用程序通知。