Home >Java >javaTutorial >How Can I Programmatically Vibrate My Android Device at a Custom Frequency?
Vibrate Your Android Device with Custom Frequency
To incorporate device vibrations into your Android application, it's necessary to leverage the VibrationEffect class.
Implementing Vibrations in Your Code
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); }
Customizing Vibration Frequency
The VibrationEffect.createOneShot() method accepts two parameters:
Note: Add the following permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
The above is the detailed content of How Can I Programmatically Vibrate My Android Device at a Custom Frequency?. For more information, please follow other related articles on the PHP Chinese website!