Vibrate Android device programmatically

Android has inbuilt functions to make a device vibrate.
You can specify a time for which the device vibrate and also check if the device can vibrate itself and other inbuilt services.
// add to the manifest file
<uses-permission android:name="android.permission.VIBRATE"/>
//add this code where you want the device to vibrate.
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 {
v.vibrate(500);
}
view raw vibrate.java hosted with ❤ by GitHub


Popular posts from this blog

Audio de-noising using Python (Wavelets)

Converting HTML5 Game for Android | 2 Methods

Display a table in JFrame UI from MySQL Table