简体   繁体   English

在Android中以编程方式禁用所有声音和振动

[英]Programatically disable ALL sound and vibration in android

I want to disable ALL sound and vibration from the android device. 我想禁用所有来自Android设备的声音和振动。 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);

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. 这适用于禁用音乐和拨入电话,但是,如注释中所述,Android的内置闹钟应用仍然可以产生声音和振动。

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 did not work for me to mute the alarm, but using setStreamVolume for STREAM_ALARM setting volume to zero did. setStreamMute无法使我静音,但是使用setStreamVolumeSTREAM_ALARM音量设置为零即可。 It is necessary to restore the alarm volume afterwards. 之后需要恢复警报音量。

For example like this: 例如这样:

AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);    
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM