6

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.

Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

2 Answers2

18

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

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • 2
    Note that `hasVibrator()` was added in API Level 11. Devices older than that are mostly going to have vibration motors, particularly if they (legitimately) have the Android Market on them. – CommonsWare Jan 14 '12 at 16:15
  • I have read the doc, and it says nothing about this. i am running on API 7 so i cant use "hasVibrator" so what happens if the vibrator is not present. – Jack Trowbridge Jan 14 '12 at 16:19
  • 1
    It'll just be ignored and not vibrate! You can use reflection to invoke the api lvl 11 call for those devices, depends how you want to implement your ideas. – Blundell Jan 14 '12 at 16:24
  • Thanks very much, one last quick question, if the user declines the permission would it just miss the code out or would it stop the app from working? – Jack Trowbridge Jan 14 '12 at 16:28
  • A user can't decline a permission. They are just stated when a user goes to download your app. – Blundell Jan 15 '12 at 01:25
3

does it not work at all? does it stop the app working?

Your vibration request should simply be ignored.

or does it not show up in the market at all?

You cannot filter out devices from the Market that lack a vibration motor. Hence, the VIBRATE permission is not one of those where if you ask for it imply a hardware feature.

or do i have to ask the phone if it has a vibrator?

You can, on API Level 11 and higher -- see Blundell's answer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So if i am working on API 7 i can just keep everything as i have it now, because it wont be filtered out in the market and the phone should just simply miss it out if it does not have a vibrator? – Jack Trowbridge Jan 14 '12 at 16:25