How to make an Android device vibrate?

Billie picture Billie · Dec 19, 2012 · Viewed 349.9k times · Source

I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?

Answer

Paresh Mayani picture Paresh Mayani · Dec 19, 2012

Try:

import android.os.Vibrator;
...
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);
}

Note:

Don't forget to include permission in AndroidManifest.xml file:

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