在Android中以编程方式禁用所有声音和振动
我想禁用所有来自Android设备的声音和振动.根据对类似问题的回答,我目前正在使用以下代码将所有音频流静音,将铃声模式设置为静音,并伪造语音通话场景:
I want to disable ALL sound and vibration from the android device. As per the answers to similar questions, I'm currently using the following code to mute all the audio streams, set the ringer mode to silent, and to fake a voice call scenario:
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//DOESNT DISABLE ALARM CLOCK
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
amanager.setStreamMute(AudioManager.STREAM_RING, true);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
amanager.setStreamMute(AudioManager.STREAM_DTMF, true);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
amanager.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
//disables vibrate and sound of ringer
amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//fakes voice call...changes alarm to single tone+vibrate
amanager.setMode(AudioManager.MODE_IN_CALL);
amanager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
这可用于禁用音乐和来电,但是,正如注释中所述,Android的内置闹钟应用仍然可以产生声音和振动.
This works for disabling music and incoming calls, but, as noted in the comments, the android's built-in alarm clock app is still able to produce sound and vibration.
有人知道如何真正禁用所有声音和振动吗?还是冒险绕过音频流,为什么闹钟应用程序看起来如此?
Does anyone know how to truly disable ALL sound and vibrations? Or venture a guess as to why the alarm clock app seems to by bypassing the audio streams?
setStreamMute
对我来说无法使警报静音,但是对 STREAM_ALARM
使用setStreamVolume
可以将音量设置为零做过.此后有必要恢复警报音量.
setStreamMute
did not work for me to mute the alarm, but using setStreamVolume
for STREAM_ALARM
setting volume to zero did. It is necessary to restore the alarm volume afterwards.
例如这样的
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);