How to make an Android device vibrate?

后端 未结 13 1922
长发绾君心
长发绾君心 2020-11-22 15:47

I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?

13条回答
  •  -上瘾入骨i
    2020-11-22 16:11

    I use the following utils method:

    public static final void vibratePhone(Context context, short vibrateMilliSeconds) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(vibrateMilliSeconds);
    }
    

    Add the following permission to the AndroidManifest file

    
    

    You can use overloaded methods in case if you wish to use different types of vibrations (patterns / indefinite) as suggested above.

提交回复
热议问题