android - Android 10 - 如何在不激活“请勿打扰”的情况下禁用“响铃音量”的振动
问题描述
在 Android 10 上,我正在尝试禁用“响铃音量”的振动。
所以我试图达到图像上的状态,我可以通过 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) {
听起来这将是达到图像状态的方式。
但是下面的代码激活了“请勿打扰”模式。尽管禁用了振动,但它还有许多其他效果:抑制我不想激活的通知等。
见下图
final AudioManager audio = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
上图中的状态是否以编程方式无法访问?
根据这个线程 -在 android 中启用静音模式而不触发请勿打扰它是无法访问的?
请勿打扰在 RINGER_MODE_SILENT 上激活
解决方案
我发现了更多关于所描述行为的信息。免打扰模式激活 - 当
- 有进入振动模式的命令
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
- 当手机(或我的模拟器)不支持振动时。
为了支持振动、静音、正常模式以及无需进入免打扰模式即可调节音量的能力, 我执行以下操作:
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);
}
推荐阅读
- php - Laravel 包含关系的关系
- javascript - div 显示在页面上,除非按下按钮,否则不应显示
- r - Valgrind/R 不工作:“致命错误:无法创建 'R_TempDir'”
- c# - 加快 N(P) 问题搜索 - 和方问题
- django - 扩展base.html时来自多个应用程序的Django静态CSS
- c# - identityReference.Translate(typeof(System.Security.Principal.NTAccount)).ToString() 未提供完整的域名
- javascript - $(...).Jcrop 不是函数
- python - 检查python中的平衡括号
- reactjs - slideToShow 未呈现指定数量的列
- javascript - 如何使链接适合其内部的组件?