Recipe Using Device Vibration

Device vibration is a common feature in all cellular phones.To control vibration on an Android device, a permission must be defined in the AndroidManifest XML file: <uses-permission android:name="android.permission.VIBRATE" />

Then, using the device vibrator is just another Android system service provided by the framework. It can be accessed using the Vibrator class:

Vibrator myVib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

With a Vibrator instance, just call the vibrate() method to start device vibration: myVib.vibrate(3000); //vibrate for 3 seconds

If needed, the cancel() method can be used to stop a vibration before it finishes: myVib.cancel(); //cancel the vibration

It is also possible to vibrate a rhythmic pattern. This is specified as a vibration-pause sequence. For example:

long[] pattern = {2000,1000,5000}; myVib.vibrate(pattern,1);

This causes the device to wait for 2 seconds, and then start a pattern of vibrating for 1 second, pausing for 5 seconds indefinitely. The second argument to the vibrate() method means the index into the pattern to start repeating at.This can be set to -1 to cause no repeat of the pattern at all.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment