Skip to content Skip to sidebar Skip to footer

Auto Silent Android Device With Timer Set My User

I have 2 buttons in my app starttime and endtime Now I want the device to turn in silent mode during the start and end time duration set by the user.....how can I do this? My code

Solution 1:

You can use the AudioManager class.

In this class you're looking for setRingerMode() function.

AudioManageraudiomanage= (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);

The values you can pass into the function are:

The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or RINGER_MODE_VIBRATE.

You have to add this into the manifest file:

android.permission.MODIFY_AUDIO_SETTINGS

I saw this here -https://stackoverflow.com/a/3738768/8214839

Solution 2:

You can use AudioManager for changing from genral mode to silent mode.

Code:

AudioManager audioManager;
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
// changing to silent mode
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

You have to give the required permission also in manifest file.

android.permission.MODIFY_AUDIO_SETTINGS

You can use the above code to set the phone to silent based on the condition that you need.

You can also change back to VIBRATE or GENERAL using AudioManager.RINGER_MODE_VIBRATE or AudioManager.RINGER_MODE_NORMAL respectively.

Post a Comment for "Auto Silent Android Device With Timer Set My User"