Cannot get the NotificationListenerService class to work

前端 未结 9 1850
梦如初夏
梦如初夏 2020-12-03 01:35

I have a android application that is trying to use the new NotificationListenerService class from api 18. I have created my own service class that inherits from this class a

9条回答
  •  眼角桃花
    2020-12-03 02:25

    In my experience, almost all of these answers are so damn close to the right solution!

    The CORE problem seems to happen during development; as you are developing your code the "Notification access" settings stop being honored when you update your app between debug sessions.

    If your APK/binary changes and your NotificationListenerService stops:

    • Rebooting fixes it.
    • Going back in to "Notification access" and disabling and re-enabling your app it fixes it.

    Hopefully this isn't a problem when updating your app through Google Play.

    As a best practice, for my app I add an overflow menu option that only shows up in non-release builds that allows me easy access to the settings:

    NotificationListener.java:

    public class NotificationListener
        extends NotificationListenerService
        implements RemoteController.OnClientUpdateListener
    {
        private static final int VERSION_SDK_INT = VERSION.SDK_INT;
    
        public static boolean supportsNotificationListenerSettings()
        {
            return VERSION_SDK_INT >= 19;
        }
    
        @SuppressLint("InlinedApi")
        @TargetApi(19)
        public static Intent getIntentNotificationListenerSettings()
        {
            final String ACTION_NOTIFICATION_LISTENER_SETTINGS;
            if (VERSION_SDK_INT >= 22)
            {
                ACTION_NOTIFICATION_LISTENER_SETTINGS = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS;
            }
            else
            {
                ACTION_NOTIFICATION_LISTENER_SETTINGS = "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS";
            }
    
            return new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS);
        }
    
        ...
    }
    

    menu_my_activity.xml:

    
    
        
    
        
    
    
    

    MyActivity.java:

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_my_activity, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        MenuItem menuItem;
        menuItem = menu.findItem(R.id.action_application_info);
        if (menuItem != null)
        {
            menuItem.setVisible(BuildConfig.DEBUG);
        }
        menuItem = menu.findItem(R.id.action_notification_settings);
        if (menuItem != null)
        {
            menuItem.setVisible(BuildConfig.DEBUG && NotificationListener.supportsNotificationListenerSettings());
        }
        return super.onPrepareOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_application_info:
                startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName())));
                return true;
            case R.id.action_notification_settings:
                startActivity(NotificationListener.getIntentNotificationListenerSettings());
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

提交回复
热议问题