Making Android phones vibrate is a good way to provide haptic feedback to users or to interact with users even when phone volume is low. This can ensure a better user experience and therefore increase the perceived integrity of your Android application.

The Vibrator Documentation describes the interface for making an Android phone vibrate. With this interface, you can cause an Android phone to vibrate in one of the following ways:

  1. Vibrate for a given length of time
  2. Vibrate in a given pattern
  3. Vibrate repeatedly until cancelled

Below are examples of these three vibrating methods.

IMPORTANT: First, Grant Vibration Permissions

Before you start adding the code necessary to cause your application to vibrate, you must first notify Android that your application expects to have permission to use the Vibrator. If you do not do this, you will receive a Force Close. Nobody likes encountering a Force Close; Do not forget this important first step!

Add the uses-permission line to your Manifest.xml file, outside of the block.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
	<uses-permission android:name="android.permission.VIBRATE"/>
	<application android:label="...">
		...
	</application>
</manifest>

Example: Vibrate for a Given Length of Time

This example is useful when a user touches your application and you would like to provide haptic feedback. This is the simplest method of vibration. I like 50 milliseconds as a good single-touch-feedback vibrate. You can experiment with different lengths (on your physical phone) to decide how long to make your vibration.

CUIDADO: This code must be called with a reference to a Context

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Vibrate for 300 milliseconds
v.vibrate(300);

Example: Vibrate in a Given Pattern

This method of vibration is useful when you need to provide a user with a one-time notification, such as receiving a text message. In this example, I have created a one-time vibration notification in the Morse Code SOS pattern.

CUIDADO: This code must be called with a reference to a Context

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// This example will cause the phone to vibrate "SOS" in Morse Code
// In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash"
// There are pauses to separate dots/dashes, letters, and words
// The following numbers represent millisecond lengths
int dot = 200;		// Length of a Morse Code "dot" in milliseconds
int dash = 500;		// Length of a Morse Code "dash" in milliseconds
int short_gap = 200;	// Length of Gap Between dots/dashes
int medium_gap = 500;	// Length of Gap Between Letters
int long_gap = 1000;	// Length of Gap Between Words
long[] pattern = {
	0, 	// Start immediately
	dot, short_gap, dot, short_gap, dot, 	// s
	medium_gap,
	dash, short_gap, dash, short_gap, dash, // o
	medium_gap,
	dot, short_gap, dot, short_gap, dot, 	// s
	long_gap
};

// Only perform this pattern one time (-1 means "do not repeat")
v.vibrate(pattern, -1);

Example: Vibrate Repeatedly Until Cancelled

This method of vibration is useful when you need to notify the user of something that requires more immediate action, such as an incoming phone call. You can repeat a given pattern until the Vibrator is cancelled, either by the system or manually by your Android application.

CUIDADO: The below example will cease vibrating when the screen times out.
CUIDADO: This code must be called with a reference to a Context

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start immediately
// Vibrate for 200 milliseconds
// Sleep for 500 milliseconds
long[] pattern = { 0, 200, 500 };

// The "0" means to repeat the pattern starting at the beginning
// CUIDADO: If you start at the wrong index (e.g., 1) then your pattern will be off --
// You will vibrate for your pause times and pause for your vibrate times !
v.vibrate(pattern, 0);

In another part of your code, you can handle turning off the vibrator as shown below:

// Stop the Vibrator in the middle of whatever it is doing
// CUIDADO: Do *not* do this immediately after calling .vibrate().
// Otherwise, it may not have time to even begin vibrating!
v.cancel();

Notes

This article only covers a subset of issues related to Vibration on the Android platform but it should provide a good starting point. Below are a few things I have noticed but did not cover in this write-up. I would appreciate comments about these, if you have any:

  • From what I have read, there is unfortunately no way of changing the vibration intensity.
  • I did not research or discuss how to make phone vibrate despite the screen timeout.
  • I have not seen the Vibrator log its activities in LogCat which makes it difficult to tweak vibration.
  • As a result, I test vibration directly on my phone which requires exporting and installing my application on my physical phone. Tedious at best.

Reference Material