Vibrating the Phone Android

Vibration is a great way to enable notifications to catch the attention of a user in noisy environments or alert the user when visible and audible alerts are not appropriate (though a vibrating phone is often noisy on a hard desktop surface).Android notifications give a fine level of control over how vibration is performed. However, before the application can use vibration with a notification, an explicit permission is needed. The following XML within your application’s AndroidManifest.xml file is required to use vibration:

<uses-permission
android:name="android.permission.VIBRATE" />

Without this permission, the vibrate functionality will not work nor will there be any error. With this permission enabled, the application is free to vibrate the phone however it wants. This is accomplished by describing the vibrate member variable, which determines the vibration pattern. An array of long values describes the vibration duration. Thus, the following line of code enabled a simple vibration pattern that occurs whenever the notification is triggered:

notify.vibrate = new long[] {0, 200, 200, 600, 600};

This vibration pattern vibrates for 200 milliseconds and then stops vibrating for 200 milliseconds. After that, it vibrates for 600 milliseconds and then stops for that long. To repeat the Notification alert, a notification flag can be set so it doesn’t stop until the user clears the notification.

notify.flags |= Notification.FLAG_INSISTENT;

An application can use different patterns of vibrations to alert the user to different types of events or even present counts. For instance, think about a grandfather clock with which you can deduce the time based on the tones that are played.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics