问题
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.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/8863363/android-vibrator-method-if-phone-has-no-vibrator