I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?
To access it, open the “Settings” app and go to Sounds And Vibration > Vibration Intensity. Tune the available sliders to adjust the vibration intensity of incoming calls, notifications, and touch interaction response to your liking.
Try:
import android.os.Vibrator; ... Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); } else { //deprecated in API 26 v.vibrate(500); }
Note:
Don't forget to include permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.VIBRATE"/>
Before you start implementing any vibration code, you have to give your application the permission to vibrate:
<uses-permission android:name="android.permission.VIBRATE"/>
Make sure to include this line in your AndroidManifest.xml file.
Most IDEs will do this for you, but here is the import statement if yours doesn't:
import android.os.Vibrator;
Make sure this in the activity where you want the vibration to occur.
In most circumstances, you'll be wanting to vibrate the device for a short, predetermined amount of time. You can achieve this by using the vibrate(long milliseconds)
method. Here is a quick example:
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 400 milliseconds v.vibrate(400);
That's it, simple!
It may be the case that you want the device to continue vibrating indefinitely. For this, we use the vibrate(long[] pattern, int repeat)
method:
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Start without a delay // Vibrate for 100 milliseconds // Sleep for 1000 milliseconds long[] pattern = {0, 100, 1000}; // The '0' here means to repeat indefinitely // '0' is actually the index at which the pattern keeps repeating from (the start) // To repeat the pattern from any other point, you could increase the index, e.g. '1' v.vibrate(pattern, 0);
When you're ready to stop the vibration, just call the cancel()
method:
v.cancel();
If you want a more bespoke vibration, you can attempt to create your own vibration patterns:
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Start without a delay // Each element then alternates between vibrate, sleep, vibrate, sleep... long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100}; // The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array v.vibrate(pattern, -1);
There are multiple SDKs that offer a more comprehensive range of haptic feedback. One that I use for special effects is Immersion's Haptic Development Platform for Android.
If your device won't vibrate, first make sure that it can vibrate:
// Get instance of Vibrator from current Context Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Output yes if can vibrate, no otherwise if (v.hasVibrator()) { Log.v("Can Vibrate", "YES"); } else { Log.v("Can Vibrate", "NO"); }
Secondly, please ensure that you've given your application the permission to vibrate! Refer back to the first point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With