How to make an Android device vibrate

0 votes
I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?
May 25, 2020 in Java by kartik
• 37,490 points
89 views

1 answer to this question.

0 votes

Hello @kartik,

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"/>

Hope this works!!

answered May 25, 2020 by Niroj
• 82,560 points

Related Questions In Java

0 votes
1 answer

How to pass an object from one activity to another on Android

One option could be letting your custom ...READ MORE

answered Jun 25, 2018 in Java by Rishabh
• 3,620 points
2,683 views
0 votes
1 answer

How to use an existing database with an Android application?

Hello @kartik, If you are having pre built ...READ MORE

answered Jul 28, 2020 in Java by Niroj
• 82,560 points
237 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,200 points
160 views
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,890 points
157 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
274 views
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,270 points
259 views
0 votes
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
1,395 views
0 votes
1 answer

How to pass an object from one activity to another on Android?

Hello @kartik, Implement your class with Serializable. Let's ...READ MORE

answered Apr 8, 2020 in Java by Niroj
• 82,560 points
118 views
0 votes
1 answer

How to launch Google Maps Directions via an intent on Android?

Hello @kartik, If you are interested in showing the ...READ MORE

answered May 25, 2020 in Java by Niroj
• 82,560 points
1,943 views