Android 10 - 如何为 "Ring Volume without activating " 请勿打扰禁用振动”

Android 10 - how to DISABLE VIBRATION for "Ring Volume without activating "do not disturb"

在 Android 10 我正在尝试禁用 "Ring Volume" 的振动。
所以我试图达到图像上的状态,我可以通过 UI - 以编程方式达到。

问题:

我尝试使用 AudioManager.setRingerMode() 它的规范说:

    /**
     * Sets the ringer mode.
     * <p>
     * Silent mode will mute the volume and will not vibrate. Vibrate mode will
     * mute the volume and vibrate. Normal mode will be audible and may vibrate
     * according to user settings.
     * <p>This method has no effect if the device implements a fixed volume policy
     * as indicated by {@link #isVolumeFixed()}.
     * * <p>From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed
     * unless the app has been granted Do Not Disturb Access.
     * See {@link NotificationManager#isNotificationPolicyAccessGranted()}.
     * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL},
     *            {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
     * @see #getRingerMode()
     * @see #isVolumeFixed()
     */
    public void setRingerMode(int ringerMode) {

听起来像是达到图像上的状态的方式。

但是下面的代码激活了 "Do not Disturb" 模式。尽管禁用了振动 还有很多其他效果:抑制我不想激活的通知等。

见下图

final AudioManager audio = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

是否是上图中的状态 - 无法以编程方式访问?
根据此线程 - Enable Silent Mode in android without triggering Do Not Disturb 无法访问?

请勿打扰在 RINGER_MODE_SILENT

激活

我发现了更多有关所描述行为的信息。 免打扰模式激活 - 当

  1. 有进入振动模式的命令audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
  2. 当 phone(或我的模拟器)不支持振动时。

支持震动、静音、普通模式, 以及调节音量的能力 无需进入免打扰模式 我执行以下操作:

private void adjustVolume(double ringAndNotificationsVolumeInPercent, int ringMode) {

    // ringer mode applies to notification
    final AudioManager audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);

     // ALWAYS set the ringer mode, cause changing the volume might implicitly change the mode
    switch (ringMode) {
        case AudioManager.RINGER_MODE_SILENT:
            /**
             * This is a tricky one:
             *
             * The goal is - to just disable Vibration and set volume to 0.
             *
             * using
             * audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
             * sets the phone to to "DontDisturb" mode,
             * having a lot of side effects: like supressing notifications etc.
             *
             * Instead - setting the phone to "RINGER_MODE_NORMAL",
             * then adjusting the volume - has the correct effect.
             */
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, AudioManager.FLAG_VIBRATE);
            break;

        case AudioManager.RINGER_MODE_VIBRATE:
            Log.d(TAG, "vibrate mode");
            if (utilsVolume.isDontDisturbOff()) {

                // if there is no vibrate mode - the OS may set the phone to DontDisturb
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            } else {
                Log.d(TAG, "Cant enable vibration mode, cause the Dont DIsturb Mode is on. It would cause ");
            }
            break;

        case AudioManager.RINGER_MODE_NORMAL:
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

            // adjust volume
            setSoundRelativeToMax(ringAndNotificationsVolumeInPercent, AudioManager.STREAM_NOTIFICATION);
            setSoundRelativeToMax(ringAndNotificationsVolumeInPercent, AudioManager.STREAM_RING);
            break;

        default:
            Log.d("TAG", "Ignore unknown RINGER_MODE " + ringMode);
            break;
    }
}

private void setSoundRelativeToMax(double percent, int streamType) {
    // requested rights in MainActivity
    final AudioManager audio = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);

    int maxVolume = audio.getStreamMaxVolume(streamType);
    int seventyVolume = (int) (maxVolume * percent);
    audio.setStreamVolume(streamType, seventyVolume, 0);
}