Android vibrate is deprecated. How to use VibrationEffect in Android>= API 26?
Solution 1
Amplitude is an int value. Its The strength of the vibration. This must be a value between 1 and 255, or DEFAULT_AMPLITUDE
which is -1.
You can use it as VibrationEffect.DEFAULT_AMPLITUDE
More details here
Solution 2
with kotlin
private fun vibrate(){
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(200)
}
}
Solution 3
You can use this for haptic feedback (vibration):
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
There are other constants available in HapticFeedbackConstants
like VIRTUAL_KEY
, KEYBOARD_TAP
...
Solution 4
Updated for Kotlin
// 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)
}
}
}
Call the function as following:
vibrateDevice(requireContext())
Make sure you add permission to AndroidManifest.xml as following:
<uses-permission android:name="android.permission.VIBRATE"/>
Note that you don't need to ask for permission at runtime for using vibration.
You need to suppress deprecation in else
clause, because the warning is from newer SDK.
Solution 5
I just stumbled across this and found out that VibrationEffect.createWaveform()
basically uses the same long[]
-pattern as the old vibrate()
.
Because of this, you can reuse your existing pattern like so (this is a Kotlin extension-function):
fun Context.vibrate(pattern: LongArray) {
val vibrator =
applicationContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator? ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(
VibrationEffect.createWaveform(pattern, VibrationEffect.DEFAULT_AMPLITUDE)
)
} else {
@Suppress("DEPRECATION")
vibrator.vibrate(pattern, -1)
}
}
And instead of VibrationEffect.createOneShot()
you can use a pattern as well (e.g. longArrayOf(0, 150)
) so no need to use different functions.
Hitesh Sahu
I am a Fullstack Feveloper working @Thoughtworks Munich. Feel free to visit my site to explore my work & experiments. Portfolio: https://hiteshsahu.com GitHub: https://github.com/hiteshsahu Codepen: https://codepen.io/hiteshsahu Linkedin: www.linkedin.com/in/hiteshsahu Twitter: https://twitter.com/HiteshSahu_
Updated on January 07, 2021Comments
-
Hitesh Sahu over 3 years
I am using Android's
VIBRATOR_SERVICE
to give a haptic feedback for a button touch.((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);
Android Studio give me warning that method
vibrate(interval)
is deprecated I should useVibrationEffect
for API>23.So I used
VibrationEffect
's methodcreateOneShot
which takes 2 params: interval, and amplitude.I tried searching for it but got no clue about what to pass as
amplitude
, anybody got any idea about how to use it?Update Added code
// Vibrate for 150 milliseconds private void shakeItBaby() { if (Build.VERSION.SDK_INT >= 26) { ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150,10)); } else { ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150); } }