Check if user has granted NotificationListener access to my app

女生的网名这么多〃 提交于 2019-12-09 11:47:48

问题


I'm using this code to open Notification Listener Settings:

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

I would like to check if the user has granted authorization to my app. I already tried to check if my NotificationListenerService is running, but it seems that it gets halted and restarted by the system (even if I return START_STICKY).


回答1:


The only correct way to do it is to check the secure settings:

ComponentName cn = new ComponentName(context, YourNotificationListenerService.class);
String flat = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
final boolean enabled = flat != null && flat.contains(cn.flattenToString());



回答2:


I was wrong, checking if service is running works:

    private boolean isNLServiceRunning() {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
             if (NLService.class.getName().equals(service.service.getClassName())) {
                  return true;
             }
        }

        return false;
    }



回答3:


If you're using androidx you can use

NotificationManagerCompat.getEnabledListenerPackages(Context context)

to check if your listener is enabled. Source



来源:https://stackoverflow.com/questions/20141727/check-if-user-has-granted-notificationlistener-access-to-my-app

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