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

时间:2013-09-15 14:10:44

标签: android android-notifications

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

据我所知,我需要通过此次调用启用对设置中通知的访问:

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

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

3 个答案:

答案 0 :(得分:14)

我设法找到一个轻微的黑客来解决这个问题。如果您查看Settings.Secure的来源,则会看到ENABLED_NOTIFICATION_LISTENERS已注明@hide。我不知道这是否是故意的。在一个相关主题上,CommonsWare确实在another answer中提到有bug阻止启动此设置,所以我猜是无意的。

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

String enabledListeners = Settings.Secure.getString(context.getContentResolver(), 
    "enabled_notification_listeners");

然后您只需检查您的服务是否在列表中。

我没有在所有API> = 18上对此进行测试,但它适用于Android 4.4.2。

答案 1 :(得分:2)

可能是API 19(4.4)及更高版本。 虽然我尝试过API 21(5.0)

String enabledListeners = Settings.Secure.getString(context.getContentResolver(), 
"enabled_notification_listeners");

“安全”正在转向RED。如果你想使用它,你必须使用:

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

答案 2 :(得分:0)

尝试:

NotificationManagerCompat.from(context).areNotificationsEnabled()