Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式启用“请勿打扰”,但有例外_Java_Android_Kotlin_Do Not Disturb - Fatal编程技术网

Java 以编程方式启用“请勿打扰”,但有例外

Java 以编程方式启用“请勿打扰”,但有例外,java,android,kotlin,do-not-disturb,Java,Android,Kotlin,Do Not Disturb,我有一个android应用程序,我需要以编程方式打开“请勿打扰”按钮,但除此之外,我可以播放媒体。我使用以下代码成功地打开了请勿打扰: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (notificationManager.isNotificationPolicyAccessGranted) { Log.d(Globals.LOG_TAG, "has permissions") } else

我有一个android应用程序,我需要以编程方式打开“请勿打扰”按钮,但除此之外,我可以播放媒体。我使用以下代码成功地打开了请勿打扰:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (notificationManager.isNotificationPolicyAccessGranted) {
        Log.d(Globals.LOG_TAG, "has permissions")
    } else {
        Log.d(Globals.LOG_TAG, "does not have permissions")
        val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
        startActivity(intent)
    }
} else {
    Log.d(Globals.LOG_TAG, "device does not support do not disturb feature")
}
并添加了权限:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>

但由于“请勿打扰”,无法在应用程序上播放媒体。但我看到我可以设置异常(对于呼叫、警报、媒体…),我想以编程方式向媒体添加异常

我怎样才能做到这一点?谢谢。

需要做什么

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
而不是

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)