Android 应用程序是否有办法检查是否允许访问通知?

Android 应用程序是否有办法检查是否允许访问通知?,android,android-notifications,Android,Android Notifications,我想实现NotificationListenerService,以访问发布到通知栏的所有通知 我知道我需要通过此呼叫启用对设置中通知的访问: startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")); 有没有办法检查用户是否激活了我的应用程序的通知访问权限?我设法找到了一个解决方法。如果您检查设置.Secure的源代码,您将看到启用的\u通知\u侦听器用@hide注释。我不知道这是不是

我想实现NotificationListenerService,以访问发布到通知栏的所有通知

我知道我需要通过此呼叫启用对设置中通知的访问:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

有没有办法检查用户是否激活了我的应用程序的通知访问权限?

我设法找到了一个解决方法。如果您检查
设置.Secure
的源代码,您将看到
启用的\u通知\u侦听器
@hide
注释。我不知道这是不是故意的。在一个相关的主题上,Commonware确实提到了一个问题,即存在一个阻止启动此设置的漏洞,因此我猜这是无意的

无论如何,为了解决这个问题,我所做的只是使用
enabled\u notification\u listeners
的字符串值获取当前的已启用通知侦听器列表:

String enabledListeners = Settings.Secure.getString(context.getContentResolver(), 
    "enabled_notification_listeners");
然后你只需要检查一下你的服务是否在列表中

我还没有在所有API>=18上测试它,但它正在Android 4.4.2上运行。

可能是在API 19(4.4)和更高版本上。 虽然我尝试了API 21(5.0)

“安全”正在变红。如果你想使用它,你必须使用:

String enabledNotificationListeners = 
  android.provider.Settings.Secure.getString(context.getContentResolver(), 
  "enabled_notification_listeners");
尝试:


检查您的服务是否在列表中非常容易,请在此处查看我的答案:4.4.4版对我不起作用,但我在Genymotion Emulatoron上尝试过。如果合并了,我们将不再需要在此处使用硬编码字符串。希望它能很快进入!这是为了检查您的通知是否在用户请求时被Android阻止。问题是关于NotificationListenerService(检查是否可以侦听通知)。
String enabledNotificationListeners = 
  android.provider.Settings.Secure.getString(context.getContentResolver(), 
  "enabled_notification_listeners");
NotificationManagerCompat.from(context).areNotificationsEnabled()