Override silent mode and/or media volume

╄→尐↘猪︶ㄣ 提交于 2019-11-29 23:32:50

问题


I want to override the silent mode and/or media volume to make the phone broadcast a loud noise. I know the alarm clock can override silent mode. How do you do this through your app?


回答1:


Are you asking whether the Alarm Clock app can override silent mode or if code can override silent mode?

The code answer is yes, you can change the silent mode setting via code like this:

AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int max = audio.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audio.setStreamVolume(AudioManager.STREAM_RING, max, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);



回答2:


I got another way, as the Tony's answer was worked but what happen it will change the profile suppose you put your device into the silent mode and app required to play any sound like alarm tone then it will change your profile silent to ringer mode and then again you need to put into silent mode. Am I right?

So the another solution was you can play the ringtone here snippet given below

int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
if(volume==0)
volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, volume,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringTonePath));
if(ringtone!=null){
    ringtone.setStreamType(AudioManager.STREAM_ALARM);
    ringtone.play();
    isRinging = true;
}

isRinging set as flag if you want to programatically stop the playing or you can check the by isPlaying() of ringtone to stop playing

going through this I didn't change the current profile and no more code for that




回答3:


Prey is an open source phone tracker app which does this as one if its features - if you don't get a better answer could have a look at the source to see how they did it.



来源:https://stackoverflow.com/questions/4071149/override-silent-mode-and-or-media-volume

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!