How to make an Android device vibrate?

后端 未结 13 2027
长发绾君心
长发绾君心 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条回答
  •  时光说笑
    2020-11-22 16:18

    Use this:

    import android.os.Vibrator;
         ...
         Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
         // Vibrate for 1000 milliseconds
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                v.vibrate(VibrationEffect.createOneShot(1000,VibrationEffect.DEFAULT_AMPLITUDE));
         }else{
         //deprecated in API 26 
                v.vibrate(1000);
         }
    

    Note:

    Don't forget to include permission in AndroidManifest.xml file:

    
    

提交回复
热议问题