检查用户是否已授予NotificationListener访问我的应用程序的权限
问题描述:
我正在使用以下代码打开通知侦听器设置":
I'm using this code to open Notification Listener Settings:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
我想检查用户是否已授权我的应用程序. 我已经尝试检查NotificationListenerService是否正在运行,但是它似乎已被系统暂停并重新启动(即使我返回了START_STICKY).
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).
答
我错了,检查服务是否正在运行:
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;
}