问题
I want to put my phone on silent mode for a shorter amount of time.
I am using the following code to put my phone on silent mode and it works. However the next segment doesn't put it back to ringer mode/previous mode.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// turn off beep sounds
mAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
mAudioManager.setStreamMute(AudioManager.STREAM_ALARM, true);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}
This to put it back on previous state:
public void destroy()
{
mAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
mAudioManager.setStreamMute(AudioManager.STREAM_ALARM, false);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
Log.d(TAG, "onDestroy"); }
I tested by exiting the app also by manually calling those codes in onDestory on a seperate methode but got no success,it stays silent.
It will be better if I can put my phone on silent mode for a small amounts of time.
Thanks.
回答1:
- Create an Alarm using
AlarmManager
when you want to put your phone to Silent Mode make your device silent that time - Assign time in
AlarmManager
when you want to stop it. Receive the alarm usingBroadcastReceiver
and stop the silent Mode.
Have a look on this Similar Question
来源:https://stackoverflow.com/questions/50033851/android-make-phone-silent-for-a-limited-amounts-of-time-dyanmically