Redirect to Notification Access Settings

后端 未结 3 1807
名媛妹妹
名媛妹妹 2021-01-03 03:30

I\'m currently developing an app for Android that uses the NotificationListenerService, which requires that the user will enable notification access for my app

相关标签:
3条回答
  • 2021-01-03 03:51

    Many Thanks to @adneal and @Waboodoo. I am posting this for an complete answer

    Check permission granted or not using this method

    private boolean isNotificationServiceRunning() {
        ContentResolver contentResolver = getContentResolver();
        String enabledNotificationListeners =
                Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
        String packageName = getPackageName();
        return enabledNotificationListeners != null && enabledNotificationListeners.contains(packageName);
    }
    

    Then show settings activity, if necessary

    boolean isNotificationServiceRunning = isNotificationServiceRunning();
    if(!isNotificationServiceRunning){
        startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
    }
    
    0 讨论(0)
  • 2021-01-03 04:01

    You can always use the notification manager to check if notifications are enabled at the OS level:

    NotificationManagerCompat.from(context).areNotificationsEnabled()
    
    0 讨论(0)
  • 2021-01-03 04:18

    You can open the NotificationAccessSettingsActivity by using the following Intent, but I'm not sure about checking to see if they've already enabled your app.

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

    Alternatively, for API 22+:

    startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS));
    
    0 讨论(0)
提交回复
热议问题