Vibrator examples – Android Programming

This article will cover how to use the Vibrator in Android.

Firstly, in your AndroidManifest.xml, you must have this line under the root element, otherwise you will get a Runtime Exception

Quick start

if you just want to vibrate it once, this is the simplest example. Depending on your usage, it can be inside your Activity, Fragment or Service. the context object can be obtained from your Activity or Service.

Vibration patterns

You can set the vibrator to vibrate with any pattern and repeat for as long as you like. (Of course you shouldn’t repeat it forever otherwise Users will be upset)

The pattern is defined in this way:

Example 1: Same as before

This code behave just the same as the previous example. In {0,500}¬†, 0 means delay for 0 ms, 500 means vibrate for 500 ms. The second parameter -1 means the pattern won’t repeat.

Example 2: Two short pulse

Example 3: Repeating Two long virbate Indefinitely

Example 4: SOS

Have fun!

Share your thoughts