Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 952
Jack Trowbridge Avatar asked Jan 14 '12 16:01

Jack Trowbridge


People also ask

Why is vibrate not working on Android?

Whether you use an iPhone or Android, your phone won't vibrate if it is in Do Not Disturb or Silent mode. If that's the case, you already know what to do: disable both of them. If your phone is in Do Not Disturb or Silent mode, you should see the icons in the status bar.


2 Answers

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

like image 184
Blundell Avatar answered Oct 12 '22 23:10

Blundell


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.

like image 20
CommonsWare Avatar answered Oct 12 '22 22:10

CommonsWare