Vibrating the phone 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.


Leave a Reply

Your email address will not be published. Required fields are marked *