在Android中以编程方式禁用所有声音和振动 [英] Programatically disable ALL sound and vibration in android

查看:649
本文介绍了在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);

这篇关于在Android中以编程方式禁用所有声音和振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆