How to make an Android device vibrate?

后端 未结 13 1727
长发绾君心
长发绾君心 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: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

    <uses-permission android:name="android.permission.VIBRATE"/>
    

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

    0 讨论(0)
  • 2020-11-22 16:13
    <uses-permission android:name="android.permission.VIBRATE"/>
    

    should be added inside <manifest> tag and outside <application> tag.

    0 讨论(0)
  • 2020-11-22 16:14

    Kotlin update for more type safety

    Use it as a top level function in some common class of your project such as Utils.kt

    // Vibrates the device for 100 milliseconds.
    fun vibrateDevice(context: Context) {
        val vibrator = getSystemService(context, Vibrator::class.java)
        vibrator?.let {
            if (Build.VERSION.SDK_INT >= 26) {
                it.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE))
            } else {
                @Suppress("DEPRECATION")
                it.vibrate(100)
            }
        }
    }
    

    And then call it anywhere in your code as following:

    vibrateDevice(requireContext())
    

    Explanation

    Using Vibrator::class.java is more type safe than using String constants.

    We check the vibrator for nullability using let { }, because if the vibration is not available for the device, the vibrator will be null.

    It's ok to supress deprecation in else clause, because the warning is from newer SDK.

    We don't need to ask for permission at runtime for using vibration. But we need to declare it in AndroidManifest.xml as following:

    <uses-permission android:name="android.permission.VIBRATE"/>
    
    0 讨论(0)
  • 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:

    <uses-permission android:name="android.permission.VIBRATE"/>
    
    0 讨论(0)
  • 2020-11-22 16:19

    Above answers are perfect. However I wanted to vibrate my app exactly twice on button click and this small information is missing here, hence posting for future readers like me. :)

    We have to follow as mentioned above and the only change will be in the vibrate pattern as below,

    long[] pattern = {0, 100, 1000, 300};
    v.vibrate(pattern, -1); //-1 is important
    

    This will exactly vibrate twice. As we already know

    1. 0 is for delay
    2. 100 says vibrate for 100ms for the first time
    3. next comes delay of 1000ms
    4. and post that vibrate again for 300ms

    One can go on and on mentioning delay and vibration alternatively (e.g. 0, 100, 1000, 300, 1000, 300 for 3 vibrations and so on..) but remember @Dave's word use it responsibly. :)

    Also note here that the repeat parameter is set to -1 which means the vibration will happen exactly as mentioned in the pattern. :)

    0 讨论(0)
  • 2020-11-22 16:19

    I struggled understanding how to do this on my first implementation - make sure you have the following:

    1) Your device supports vibration (my Samsung tablet did not work so I kept re-checking the code - the original code worked perfectly on my CM Touchpad

    2) You have declared above the application level in your AndroidManifest.xml file to give the code permission to run.

    3) Have imported both of the following in to your MainActivity.java with the other imports: import android.content.Context; import android.os.Vibrator;

    4) Call your vibration (discussed extensively in this thread already) - I did it in a separate function and call this in the code at other points - depending on what you want to use to call the vibration you may need an image (Android: long click on a button -> perform actions) or button listener, or a clickable object as defined in XML (Clickable image - android):

     public void vibrate(int duration)
     {
        Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibs.vibrate(duration);    
     }
    
    0 讨论(0)
提交回复
热议问题