How can I find out if a device has a vibrator?

回眸只為那壹抹淺笑 提交于 2019-12-18 18:34:10

问题


I have a device of which I don't know if it has a vibrator.

Is there a way to query for the availability of the vibrator?


回答1:


The Vibrator class does just that. It's hasVibrator() method returns a boolean indicating if vibrating is supported.

  1. Get an instance of the Vibrator class which is a system service.
  2. Query the Vibrator class using the hasVibrator() method.
String vs = Context.VIBRATOR_SERVICE;
Vibrator mVibrator = (Vibrator)getSystemService(vs);

boolean isVibrator = mVibrator.hasVibrator();



回答2:


This may help for API<11:

Context.getSystemService() returns a service object or null if no service.

if ( getSystemService(VIBRATOR_SERVICE) != null ) {
    //Vibrator exists
}



回答3:


You need to support (at least) Android 3.0 (11 HoneyComb) before you can use hasVibrator().

Oddly, you can use vibrate() itself on any/all versions.

So the REAL question is: How do v1 - v10 detect if the device has vibrator? (Or will nothing bad happen if you try to vibrate a device without a vibrator?)



来源:https://stackoverflow.com/questions/6801574/how-can-i-find-out-if-a-device-has-a-vibrator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!