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.
You can specify a time for which the device vibrate and also check if the device can vibrate itself and other inbuilt services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |