Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling and disabling vibration in android programmatically

Tags:

android

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

like image 271
NoOne Avatar asked Dec 30 '12 00:12

NoOne


1 Answers

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);
    }
}
like image 71
gnobal Avatar answered Oct 24 '22 00:10

gnobal