Android: vibrator method (if phone has no vibrator?)

Jack Trowbridge picture Jack Trowbridge · Jan 14, 2012 · Viewed 7.1k times · Source

I want to use the vibrator method in my app, and i have got it working on my phone which has a vibrator which is great. however phones that don't have a vibrator what happens. does it not work at all? does it stop the app working? or does it not show up in the market at all? or do i have to ask the phone if it has a vibrator?

I would also like to know if this code is good or needs any adjustments? here is my code..

Vibrator vi;

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

vi.vibrate(100);

<uses-permission android:name="android.permission.VIBRATE" /> (In manifest)

Thanks, any help would be great.

Answer

Blundell picture Blundell · Jan 14, 2012

Check the docs, http://developer.android.com/reference/android/os/Vibrator.html

all you need to do is check if a vibrator is present on the phone like so:

 Vibrator vi;

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

 if(vi.hasVibrator()){
     vi.vibrate(100);
 }

Because of the vibrate permission Android market may filter your app to just phones with a vibrate. To avoid this you can use the tag with the attribute of required="false"

 <uses-permission android:name="android.permission.VIBRATE" />
 <uses-feature android:name="there.isnt.a.vibrate.feature" android:required="false" />

It's all documented here:

http://developer.android.com/guide/topics/manifest/uses-permission-element.html

http://developer.android.com/guide/topics/manifest/uses-feature-element.html

HOWEVER

There is not a Vibrate feature string, therefore Android Market Will Not filter your app because you are using the vibrate permission. So your ok to just use uses-permission and do the check in the Java code.

Devices need a vibrator to be compatible with the android market, but of course this doesn't go for the amazon and other app markets (Barnes & Noble Nook doesn't have a vib).

This is backed up by Dianne Hackthorn (Android lead dev at Google's) reply to this thread: http://groups.google.com/group/android-developers/browse_thread/thread/7713e796ea2d0f5f