This is how you can use it today, as it will try to go to as few steps for the user as possible, to turn it on:
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
fun getIntentForNotificationAccess(packageName: String, notificationAccessServiceClass: Class<out NotificationListenerService>): Intent =
getIntentForNotificationAccess(packageName, notificationAccessServiceClass.name)
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
fun getIntentForNotificationAccess(packageName: String, notificationAccessServiceClassName: String): Intent {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Intent(Settings.ACTION_NOTIFICATION_LISTENER_DETAIL_SETTINGS)
.putExtra(Settings.EXTRA_NOTIFICATION_LISTENER_COMPONENT_NAME, ComponentName(packageName, notificationAccessServiceClassName).flattenToString())
}
val intent = Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
val value = "$packageName/$notificationAccessServiceClassName"
val key = ":settings:fragment_args_key"
intent.putExtra(key, value)
intent.putExtra(":settings:show_fragment_args", Bundle().also { it.putString(key, value) })
return intent
}
Example usage:
startActivityForResult(getIntentForNotificationAccess(packageName, NotiService::class.java), REQUEST_CODE)
or:
startActivityForResult(getIntentForNotificationAccess(packageName, NotiService::class.java.name), REQUEST_CODE)
Note that sadly this isn't possible for various other cases, sadly. Not even highlight&focus. For these cases, I've made these requests: