Android - make phone silent for a limited amounts of time (dyanmically)

本小妞迷上赌 提交于 2020-01-06 05:42:07

问题


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 using BroadcastReceiver 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

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