Home > Java > javaTutorial > How Can I Programmatically Vibrate My Android Device at a Custom Frequency?

How Can I Programmatically Vibrate My Android Device at a Custom Frequency?

Susan Sarandon
Release: 2024-11-29 03:20:08
Original
109 people have browsed it

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);
}
Copy after login

Customizing Vibration Frequency

The VibrationEffect.createOneShot() method accepts two parameters:

  • Duration: Specifies the duration of the vibration in milliseconds. The example code causes a 500-millisecond vibration.
  • Amplitude: Defines the vibration intensity. By default, it's set to VibrationEffect.DEFAULT_AMPLITUDE, but you can customize it for stronger or weaker vibrations.

Note: Add the following permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.VIBRATE"/>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template