2

I'm trying to implement haptic feedback when changing a value of a seekbar. It works correctly on Android pre-P. On Android P it doesn't work at all. Code:

private val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator?
private val effect by lazy { VibrationEffect.createOneShot(VIBRATION_DURATION, 50)}
...

fun vibrate() {
    if (vibrator == null || !vibrator.hasVibrator()) {
        return
    }
    vibrator.cancel()
    vibrator.vibrate(effect)
Mike
  • 2,547
  • 3
  • 16
  • 30

3 Answers3

6

It turns out the user has to enable Touch vibration in Settings -> Accessibility -> Vibration -> Touch vibration:enter image description here

Without it, short vibration (less than 5 seconds) won't work. For me, this is not quite intuitive, so I decided to post it here

Mike
  • 2,547
  • 3
  • 16
  • 30
2

exactly what Mike said. but for android Pie - Setting->Sounds and vibration -> System sounds and vibration -> Touch vibration

kfir
  • 635
  • 8
  • 27
1

You can use vibrate(VibrationEffect vibe, VibrationAttributes attributes)

(Note: Before API level 33 you need to use AudioAttributes instead of Vibration Attributes)

By setting the Usage of the VibrationAttributes/AudioAttributes for example to USAGE_NOTIFICATION you should at least have a vibration that is enabled by default. The user can still turn off the notification vibrations in the settings though.

ndreisg
  • 1,119
  • 13
  • 33