Is there a way an app can check if it is allowed to access notifications?

末鹿安然 提交于 2019-12-06 17:54:38

问题


I want to implement an NotificationListenerService to get access to all notifications that are posted to the notification bar.

I understand that I need to enable access to the Notifications in the Settings with this call:

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

Is there a way to check if the user activated Notification access for my app?


回答1:


I managed to find a slight hack to solve this. If you check the source of Settings.Secure, you will see that ENABLED_NOTIFICATION_LISTENERS is annotated with @hide. I don't know if this is intentional or not. On a related topic, CommonsWare does mention in another answer that there is a bug preventing the starting of this setting so I guess is unintentional.

Anyway, to work around this, all I did was to fetch the current list of enabled notification listeners using the string value of ENABLED_NOTIFICATION_LISTENERS:

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

Then you just have to check if your service is in the list.

I did not test this on all API >= 18 yet but it is working on Android 4.4.2.




回答2:


Probably for API 19 (4.4) and higher. Although I tried on API 21 (5.0)

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

"secured" was turning RED. If you want to use it you have to use:

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



回答3:


Try:

NotificationManagerCompat.from(context).areNotificationsEnabled()


来源:https://stackoverflow.com/questions/18813321/is-there-a-way-an-app-can-check-if-it-is-allowed-to-access-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!