How to programmatically Mute/Silent devices running Lollipop?

Prashant Patel picture Prashant Patel · Sep 14, 2015 · Viewed 8.1k times · Source

How I can make my phone mute in lollipop devices programmatically? For pre-lollipop devices, it can be done pretty well using AudioManager class but In android 5.0 and above, the device can be kept on Vibrate mode but I am not able to mute it. Can anyone help me out that how can we programmatically change phone's volume modes between NONE, PRIORITY and ALL?

Thanks.

Answer

Tushar Saha picture Tushar Saha · Sep 17, 2015

use this code:

final AudioManager mode = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    mode.setRingerMode(AudioManager.RINGER_MODE_SILENT);

and give this permission in manifest

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

i tried it on android 5.0.1 in an onclick event. it sets phone in priority then triggering the onclick again will place the phone in no interruption.