我正在尝试在更改搜索栏的值时实现触觉反馈。它可以在 Android pre-P 上正常工作。在 Android P 上它根本不起作用。代码:
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)