5

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);
}
Melquiades
  • 8,496
  • 1
  • 31
  • 46
Arun kumar
  • 1,894
  • 3
  • 22
  • 28
  • http://stackoverflow.com/questions/14087322/enabling-and-disabling-vibration-in-android-programmatically – MrDumb Mar 23 '15 at 10:54
  • possible duplicate of [Enable/disable vibration function in android?](http://stackoverflow.com/questions/10014005/enable-disable-vibration-function-in-android) – J Richard Snape Mar 23 '15 at 10:57
  • Hi Richard ,I am not taking about start vibration , I want to enable and disable it ., In the link "Enable/disable vibration function in android?" they answered for starting the vibration. not for enable and disable it . – Arun kumar Mar 23 '15 at 11:36

4 Answers4

7

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);
Arun kumar
  • 1,894
  • 3
  • 22
  • 28
5

First of all use this permission in AndroidManifest.xml

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

Now

public void startVibrate(View v) {
  long pattern[] = { 0, 100, 200, 300, 400 };
  vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  vibrator.vibrate(pattern, 0);
 }

  public void stopVibrate(View v) {
  vibrator.cancel();
 }

Vibrate pattern public abstract void vibrate (long[] pattern, int repeat) Pattern for vibration is nothing but an array of duration's to turn ON and OFF the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator ON. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values, alternates between ON and OFF.

long pattern[]={0,100,200,300,400};

If you feel not to have repeats, just pass -1 for 'repeat'. To repeat patterns, just pass the index from where u wanted to start. I wanted to start from 0'th index and hence I am passing 0 to 'repeat'.

vibrator.vibrate(pattern, 0);
Umesh Chauhan
  • 1,460
  • 1
  • 17
  • 26
  • 1
    this is only starting the vibration , we not need to start vibration , only need to enable and disable it . – Arun kumar Mar 24 '15 at 05:41
4
 myAudioManager.setVibrateSetting();

This method was deprecated in API level 16.

you can use this one:

audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)

RINGER_MODE_SILENT : will mute the volume and will not vibrate.

RINGER_MODE_VIBRATE: will mute the volume and vibrate.

RINGER_MODE_NORMAL: will be audible and may vibrate according to user settings.

Adeeb karim
  • 292
  • 3
  • 9
0
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
    //deprecated in API 26 
    v.vibrate(500);
}
Dishant Kawatra
  • 638
  • 7
  • 5