Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically Mute/Silent devices running Lollipop?

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.

like image 864
Prashant Patel Avatar asked Oct 20 '22 02:10

Prashant Patel


2 Answers

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.

like image 105
Tushar Saha Avatar answered Oct 21 '22 16:10

Tushar Saha


Replace RINGER_MODE_SILENT with RINGER_MODE_VIBRATE

AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
like image 31
iqbal lone Avatar answered Oct 21 '22 15:10

iqbal lone