【问题标题】:Turn on do not disturb programmatically, with exceptions以编程方式打开请勿打扰,但有例外
【发布时间】:2019-11-09 10:26:13
【问题描述】:

我有一个 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")
}
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)

并添加权限:

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

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

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

【问题讨论】:

    标签: java android kotlin do-not-disturb


    【解决方案1】:

    需要做的

    notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
    

    而不是

    notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)
    

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多