Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable and disable vibration mode programmatically

I need to enable and disable the vibration mode of mobile when user turns off and turns on the switch button .

I have tried the code below, but it's not working:

AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

Toast.makeText(this, "in setting "+(myAudioManager.getMode()==AudioManager.RINGER_MODE_VIBRATE),1).show();

if(myAudioManager.getMode()==AudioManager.RINGER_MODE_VIBRATE) {
    //myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
}
else
{
    //myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    myAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
}
like image 715
Arun kumar Avatar asked Dec 09 '22 04:12

Arun kumar


1 Answers

We can enable and disable the silent mode programmatically by using AudioManager:

 AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

for setting silent mode :

audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

For normal mode :

audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
like image 72
Arun kumar Avatar answered Mar 03 '23 02:03

Arun kumar