Android Development
Vibration API
Programming Tutorials
Mobile App Development
Device Hardware Control

How to make an Android device vibrate? with different frequency?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Vibrations in Android devices are typically used to provide tactile feedback to the user. This can be in response to certain events or actions, such as receiving a notification, pressing a button, or as part of a user interface to indicate errors or other important interactions. Android provides a simple way to control the vibration motor programmatically, and with the right permissions, developers can customize the vibration pattern, intensity, and frequency.

Basics of Android Vibration API

Android's primary API to control the vibration is found in the Vibrator class. To use it in an Android application, the following permission must be declared in the AndroidManifest.xml:

xml
<uses-permission android:name="android.permission.VIBRATE"/>

Once you've added the permission, you can obtain an instance of the Vibrator service like this:

java
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

Before you proceed to use the vibrator service, it’s a good practice to check whether the device has a vibrator:

java
1if (vibrator.hasVibrator()) {
2    // Vibrator available
3} else {
4    // Vibrator not available
5}

Creating Custom Vibration Patterns

For controlling the vibration patterns and frequency, you can use the methods provided by the Vibrator class. A basic vibration can be initiated using:

java
vibrator.vibrate(500); // Vibrate for 500 milliseconds

For more control, Android allows you to define a pattern.

java
long[] pattern = {0, 100, 1000, 300}; // Start without delay, vibrate for 100 ms, pause for 1000 ms, then vibrate for 300 ms.
vibrator.vibrate(pattern, -1); // -1 means do not repeat, just run once

Vibration Effects in Android 8.0 and Higher

From Android 8.0 (API level 26), the Vibrator class was supplemented with the VibrationEffect class, that allows you to define vibration patterns with different timing, amplitude, and frequencies. This is useful for setting precise control over how the vibration feels.

Here's an example of creating a vibration effect:

java
VibrationEffect effect = VibrationEffect.createWaveform(pattern, amplitudes, -1);
vibrator.vibrate(effect);

Where amplitudes is an array of amplitude values corresponding to the timing in the pattern. Amplitudes range from 0 to 255, or VibrationEffect.DEFAULT_AMPLITUDE for the default system setting.

Modifying Frequencies

Frequencies of vibration are indirectly controlled through the pattern timings and amplitudes. Increasing the amplitude while reducing the duration can create the perception of a higher frequency and a more 'intense' vibration.

Advanced Effects

For finer control, particularly on newer devices, you can use the VibrationEffect.createPredefined() method which includes effects like EFFECT_TICK or EFFECT_DOUBLE_CLICK which are optimized for common interaction types:

java
VibrationEffect effect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK);
vibrator.vibrate(effect);

Summary Table

MethodDescriptionAPI Level
vibrate(long)Basic method for simple vibrations.API 1+
vibrate(long[])Support for pause and play patterns.API 1+
VibrationEffectAdvanced customization including amplitudes and waveforms.API 26+

Conclusion

Using the Android vibration API enables developers to enhance user experience through tactile feedback. It is important to design these experiences thoughtfully, considering the context and frequency of the alerts, to avoid creating a disruptive experience. Whether providing subtle feedback or alert notifications, controlling the intensity and frequency effectively will result in a more polished and enjoyable user experience.


Course illustration
Course illustration

All Rights Reserved.