Android Vibration APIs

617 views Asked by At

I am experimenting with sound manager application to create different sound profiles for "RING, NOTIFICATION, MEDIA, SYSTEM".

As per setting the volume level, I am succeeded with it by using

AudioManager setStreamVolume(AudioManager.STREAM_RING, <value>);

The challenge comes with Vibration Settings.

Currently the api call:

AudioManager setVibrateSetting(vibrateType, vibrateSetting);

Is deprecated in API level 16 and no hope to use it..

The other alternatives is to use

AudioManager setRingerMode(AudioManager.RINGER_MODE_NORMAL);

But I am precisely looking to turn off and turn on vibration for

  • RINGER
  • NOTIFICATIONS

from my custom application. Or turn on for one of them at any time.

You can think I am doing something similar to the enabling and disabling vibration in sound settings from the OS settings option.

Any help?

2

There are 2 answers

1
AndroidHacker On

For your concern firstly you need to define in Manifest file like this

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

And use like this in your class file. Wonder where exactly you need to put this.

// You need to define some sort of pattern for this
    Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    long[] pattern = {0, 1000, 0};
    //long[] pattern = {startTime, TotalTimeDuration, not vibrating for 0ms}; .. Example for above stuff
    vb.vibrate(pattern, 0);

For Example:

button.setOnTouchListener(new OnTouchListener() {
    Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                long[] pattern = {0, 1000, 0};
                vb.vibrate(pattern, 0);
                break;
            case MotionEvent.ACTION_UP:
                vb.cancel();
                break;
        }
        return false;
    }
});
0
chaitanya On

Try this:

audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);