Vibrating the phone in Android

by Krishnaraj Varma on June 27, 2010

in Android

To vibrate the phone we use Vibrator class. This is a system service so we don’t instantiate but use getSystemService to get the instance of the class. The following code snippet vibrates the phone for a period of time.

Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
long[] pattern = {0,1000,2000,3000};

vibrator.vibrate(pattern, -1);

The vibrate method accepts the pattern to vibrate and an index of the pattern to start repeating. If we don’t want to repeat pass -1. If we just want to vibrate for a period of time with default pattern then we can use the following variant of the vibrate method which vibrate for 1 second.

vibrator.vibrate(1000);

Previous post:

Next post:

Disclaimer: This is a personal weblog. The information in this weblog is provided “AS IS” with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.