Development issue/problem:
I use Android’s VIBRATOR_SERVICE to provide haptic feedback for a single keystroke.
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300) ;
Android Studio warns me that the vibrate(interval) method is outdated, I should use VibrationEffect for API>23.
So I used the VibrationEffect createOneShot method, which takes 2 parameters: Interval and amplitude.
I tried to find it but I had no idea what the amplitude was, does anyone know how to use it?
Code update added
// Vibration for 150 milliseconds
private void shakeItBaby() {
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150,10));
} otherwise {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(150);
}
}.
How can I solve this problem?
Solution 1:
The amplitude is the int value. It’s the power of vibration. It must be a value between 1 and 255, or DEFAULT_AMPLITUDE, which is -1.
You can use it as VibrationEffect.DEFAULT_AMPLITUDE.
More information can be found here
Solution 2:
with Kotlin
private fun vibrate(){
fall 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:
It can be used for haptic (vibration) feedback:
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) ;
There are other constants in HapticFeedback Constants such as 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 {as (Build.VERSION.SDK_INT >= 26) {the.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE)))} else {@Suppress(DEPRECATION)it.vibrate(100)}}}}}
Call the function as follows:
vibrateDevice (requireContext())
Make sure you add the permission for AndroidManifest.xml as follows:
Note that you do not need to ask permission while using the vibrations.
The vacuum must be removed in another place, as a warning for the new SDK.
Solution No 5:
I encountered this problem and discovered that VibrationEffect.createWaveform() uses about the same long template as the old vibrate().
This allows you to reuse an existing model (this is a feature of the Kotlin extension):
nice Context.vibrate(pattern: LongArray) {
fall 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)
)
} otherwise {
@Delete(DEPRECATION)
vibrator.vibrate(pattern, -1)
}
}.
And instead of VibrationEffect.createOneShot() you can also use a pattern (e.g. longArrayOf(0, 150)) so that it is not necessary to use different functions.
Solution No 6:
This library can help you:
https://github.com/josephrubin/Rumble-4-Android
All you need is
Rumble.once(150) ;
It manages the API versions for you.
Solution No 7:
NuGet Open Package Management
Search and install Xamarin.Essentials
try {
var duration = TimeSpan.FromMilliseconds(300);
Vibrate.Vibrate(duration);
}
catch (FeatureNotSupported Exception ex){}
catch (Exception ex){}.
Solution No 8:
work for me Kotlin Extra Fun
for the tactile effect, the vibration has 5 milliseconds!!! (DURATION OF HAPTIC FEEDBACK)
fun Context.performHapticFeedback() { vibrator
fall = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
as (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { vibration effect
fall vibrationEffect = vibration effect.createOneShot(HAPTIC_FEEDBACK_DURATION, VibrationEffect.DEFAULT_AMPLITUDE)
vibrator.vibrate(vibrationEffect)
} else {
vibrator.vibrate(TimeUnit.MILLISECONDS.toMillis(SHORT_HAPTIC_FEEDBACK_DURATION)
}
}.
private const val SHORT_HAPTIC_FEEDBACK_DURATION = 5L
with
addOnItemTouchListener(ItemTouchListener { position, event ->
if (event.action == MotionEvent.ACTION_DOWN)))) {
context.performHapticFeedback()
}
})
Approval
Good luck ✌ :))))))
Good luck!
Related Tags:
android sdk vibrationeffect,android java vibrationeffect,android haptic api,android developer vibration pattern,vibrate android app,vibration pattern android,android vibrate oneshot,make vibrate android,vibrate android os vibrationeffect android media audioattributes,vibrate on long press android,vibration api android