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:
Once you've added the permission, you can obtain an instance of the Vibrator service like this:
Before you proceed to use the vibrator service, it’s a good practice to check whether the device has a vibrator:
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:
For more control, Android allows you to define a pattern.
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:
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:
Summary Table
| Method | Description | API Level |
vibrate(long) | Basic method for simple vibrations. | API 1+ |
vibrate(long[]) | Support for pause and play patterns. | API 1+ |
VibrationEffect | Advanced 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.

