Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
Making the Device VibrateBack to Android Cookbook Home | Up to Chapter: System and Device Control

Author: Rachee Singh ('racheesingh')
In Published Edition? Yes
FormatLanguage: WikiFormat

Making the Device Vibrate


Through your application, you wish to notify the user of an event by means of device's vibration.


Using Notifications to set a vibration pattern.


To use device vibration, include this permission in the AndroidManifest file:

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

In the Java code, we need to get an instance of the NotificationManager class and of Notification class:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
Notification notification = new Notification();

To set a pattern for the vibration, assign a sequence of long values (time in millisecond) to Notification's vibrate property. This sequence represents the time for which the device will be silent and the time for which it will vibrate, alternatively, and should be an even number of long values. For instance: The pattern used in this example will cause the device to vibrate for 1 second then pause for 1 second then vibrate again for 1 second and so on.

notification.vibrate = new long[]{1000, 1000, 1000, 1000, 1000, 1000}; 
notificationManager.notify(0, notification);

See Also



The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/Vibrate