Home >Java >javaTutorial >How Can I Control Vibration Frequency and Patterns in My Android App?
Vibrating Android Devices Effectively: Varying Frequencies and Patterns
In the realm of Android development, enhancing user experience often requires engaging multiple sensory channels. Vibrations, for instance, can provide subtle yet impactful feedback for various actions and notifications. This guide will explore how to control the vibration of an Android device, including customizing its frequency.
Triggering Vibration
To initiate vibration upon specific events within your application, follow these steps:
Customizing Frequency
Android provides multiple ways to control the vibration's frequency:
Sample Code:
import android.os.Vibrator; import android.os.Build; ... 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); }
Permission Declaration
To enable vibration functionality, don't forget to declare the android.permission.VIBRATE permission in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
Harnessing the power of vibration in your Android applications enhances user interaction and provides a more immersive experience. Whether you opt for a simple burst or intricately timed patterns, the methods described above empower you with precise control over device vibrations.
The above is the detailed content of How Can I Control Vibration Frequency and Patterns in My Android App?. For more information, please follow other related articles on the PHP Chinese website!