不推荐使用Android振动.如何在Android> = API 26中使用VibrationEffect? [英] Android vibrate is deprecated. How to use VibrationEffect in Android>= API 26?

查看:492
本文介绍了不推荐使用Android振动.如何在Android> = API 26中使用VibrationEffect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android的 VIBRATOR_SERVICE 来提供按键触摸的触觉反馈.

I am using Android's VIBRATOR_SERVICE to give a haptic feedback for a button touch.

 ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(300);

Android Studio提醒我不建议使用方法 vibrate(interval),我应该对API> 23使用 VibrationEffect .

Android Studio give me warning that method vibrate(interval) is deprecated I should use VibrationEffect for API>23.

因此,我使用了 VibrationEffect 的方法 createOneShot ,该方法需要两个参数:时间间隔和幅度.

So I usedVibrationEffect's method createOneShot 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?

更新已添加的代码

// 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);
    }
}

推荐答案

振幅是一个int值.它的振动强度.该值必须介于1到255之间,或者 DEFAULT_AMPLITUDE 为-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.

您可以将其用作 VibrationEffect.DEFAULT_AMPLITUDE

更多详细信息此处

这篇关于不推荐使用Android振动.如何在Android> = API 26中使用VibrationEffect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆