Skip to content

Instantly share code, notes, and snippets.

@yasincidem
Created August 11, 2022 13:38
Show Gist options
  • Save yasincidem/9a7dc3e7918fd361f25eff96ab4c8c98 to your computer and use it in GitHub Desktop.
Save yasincidem/9a7dc3e7918fd361f25eff96ab4c8c98 to your computer and use it in GitHub Desktop.
fun Context.vibrate() {
val vibrator: Vibrator = try {
getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
} catch (exception: ClassNotFoundException) {
exception.recordException()
null
} ?: return
when {
VERSION.SDK_INT >= VERSION_CODES.Q -> {
VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK).also(vibrator::vibrate)
}
else -> {
VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE).also(vibrator::vibrate)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment