Check for access to notifications using NotificationListenerService

后端 未结 3 525
说谎
说谎 2020-11-30 22:28

I\'m using the >=4.3 NotificationListenerService to access notifications. On the first start, my app takes the user to the \"Access Notifications\" system panel

相关标签:
3条回答
  • 2020-11-30 23:01

    Im developer of Krome. What have I done to check if service is enabled is add public static variable that changes to true in onBind method and to false in unbind. That is how this service work.

    Edit:

    public static boolean isNotificationAccessEnabled = false;
    
    @Override
    public void onListenerConnected() {
        isNotificationAccessEnabled = true;
    }
    
    @Override
    public void onListenerDisconnected() {
        isNotificationAccessEnabled = false;
    }
    
    0 讨论(0)
  • 2020-11-30 23:07

    Works well with slightly modified @Damians answer

    public class NotifyListener extends NotificationListenerService{    
            public static boolean listnerConnected = false;
        @Override
            public IBinder onBind(Intent intent) {
                Log.d(name,"onBind Called");
                listnerConnected = true;
                return super.onBind(intent);
        }   
    
            @Override
            public void onDestroy()
            {       
                super.onDestroy();        
                Log.e("destroy", "called");
                listnerConnected = false;
    
            }
    }
    
    0 讨论(0)
  • 2020-11-30 23:11

    Edit June 15th, 2016

    I'm not sure which version of the support library this was added to, but it looks like this functionality is now built in. Simply use:

    NotificationManagerCompat.getEnabledListenerPackages(context); (link to docs)

    This returns a Set<String> that you can iterate through to find your package name. Note however that I haven't personally tested this. But it looks like it's probably preferred to use this in place of my old solution below.


    Old Solution

    This code is working for my app:

    ContentResolver contentResolver = context.getContentResolver();
    String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
    String packageName = context.getPackageName();
    
    // check to see if the enabledNotificationListeners String contains our package name
    if (enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName))
    {
        // in this situation we know that the user has not granted the app the Notification access permission
        throw new Exception();
    }
    else
    {
        doSomethingThatRequiresNotificationAccessPermission();
    }
    

    Typical values that I've seen for the enabledNotificationsListeners String look like this:

    • User has given none of their apps Notification access permission
      • null or ""
    • User has given one app Notification access permission
      • "com.woodblockwithoutco.remotecontrollerexample/com.woodblockwithoutco.remotecontrollerexample.RemoteControlService"
    • User has given two apps Notification access permission
      • "com.scootrnova.android/com.scootrnova.android.ListenerService:com.woodblockwithoutco.remotecontrollerexample/com.woodblockwithoutco.remotecontrollerexample.RemoteControlService"

    This implementation is very straightforward and works great :)

    P.S. I got the idea to use the hardcoded "enabled_notification_listeners" String from this answer.

    0 讨论(0)
提交回复
热议问题