开发者

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

I have a开发者_StackOverflow中文版 device of which I don't know if it has a vibrator.

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


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();


This may help for API<11:

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

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


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?)


Maybe it'll help someone. Since I'm building a PWA, I ended up checking the screen size:

  if (window.innerWidth < 600) {
    window.navigator.vibrate(300);
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜