8

In one of my android apps, I need to enable or disable the vibration completely irrespective of the mode (i.e. silent or general or loud).

I am currently using the following code with the help of the deprecated function setVibrateSetting

// For turning on the vibration mode

audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
            AudioManager.VIBRATE_SETTING_ON);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
            AudioManager.VIBRATE_SETTING_ON);

// For turning off the vibration mode

audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
            AudioManager.VIBRATE_SETTING_OFF);
audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
            AudioManager.VIBRATE_SETTING_OFF);

But I am wondering if there is a clear way to do this.

And suggestions are most welcome :)

Regards, Jujare

NoOne
  • 313
  • 1
  • 4
  • 11

4 Answers4

5

The deprecation text for setVibrateSetting() says:

This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.

From what I understand, there is no other option to globally enable or disable vibration, so if you have an app such as a ring profile manager, you probably need to use it.

IMHO, Google used deprecation here inappropriately.

I use the following class to hide the deprecation in one "compat" class:

@SuppressWarnings("deprecation")
class AudioManagerCompat {
    final static int VIBRATE_TYPE_RINGER = AudioManager.VIBRATE_TYPE_RINGER;
    final static int VIBRATE_TYPE_NOTIFICATION = AudioManager.VIBRATE_TYPE_NOTIFICATION;
    final static int VIBRATE_SETTING_ON = AudioManager.VIBRATE_SETTING_ON;
    final static int VIBRATE_SETTING_OFF = AudioManager.VIBRATE_SETTING_OFF;
    final static int VIBRATE_SETTING_ONLY_SILENT = AudioManager.VIBRATE_SETTING_ONLY_SILENT;

    static int getVibrateSetting(AudioManager am, int vibrateType) {
        return am.getVibrateSetting(vibrateType);
    }

    static void setVibrateSetting(AudioManager am, int vibrateType, int vibrateSetting) {
        am.setVibrateSetting(vibrateType, vibrateSetting);
    }
}
gnobal
  • 18,309
  • 4
  • 30
  • 35
  • 3
    `IMHO, Google used deprecation here inappropriately.` You are right because there is no other way to change system wide vibration settings. But the word `deprecation` is always confusing for developers. Deprecation means there is a better way available rather than you can't use it! – Muhammad Babar Sep 24 '14 at 12:17
1

Here is my code

switch (position) {
    case 0:
        audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        break;
    case 1:
        audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
        break;
    case 2:
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 
                AudioManager.VIBRATE_SETTING_OFF);
        break;
    case 3:
        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 
                AudioManager.VIBRATE_SETTING_ON);
        break;
    }

setVibrateSetting works fine when I use my nexus s with Gingerbread and IceCreamSandwich, I develop it with ICS, however recently I found it doesn't work with Jelly Bean, and getVibrateSetting also doesn't work. Waiting for a better way~

Xiaochen Wang
  • 83
  • 1
  • 7
1

You can use the following code lines:

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

vibration.cancel();
julianfperez
  • 1,726
  • 5
  • 38
  • 69
swati
  • 11
  • 1
-3

Check this, i found that as the first google result. The code:

if(isVibrate){
Vibrator vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);  
     vib.vibrate(500);
      Toast.makeText(this, "vib started", Toast.LENGTH_LONG).show();
}

else{

// do nothing

}
Community
  • 1
  • 1
xacobe97
  • 51
  • 1
  • 5
  • Thanks for the code. But, my question was to disable or enable the vibration on a system level. My question is not about how I can use the vibrator device. – NoOne Dec 30 '12 at 18:35