Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (10.2k points)
I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?

1 Answer

0 votes
by (46k points)

It's too simple, just 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);

}

Also add permission in AndroidManifest.xml file:

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

Related questions

0 votes
1 answer
asked Oct 31, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...