kotlin Android振动已过时,如何在Android>= API 26中使用振动效果?

ifmq2ha2  于 2023-03-09  发布在  Kotlin
关注(0)|答案(9)|浏览(489)

我正在使用Android的VIBRATOR_SERVICE为按钮触摸给予触觉反馈。

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

Android Studio给予我,方法vibrate(interval)已弃用,我应该将VibrationEffect用于API〉23。
所以我使用了VibrationEffect的方法createOneShot,它有两个参数:间隔和振幅。

我试着搜索它,但没有线索什么传递为amplitude,有人知道如何使用它吗?

更新添加的代码

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

jv4diomz1#

与Kotlin

private fun vibrate(){
    val 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)
    }
}
7bsow1i6

7bsow1i62#

振幅是一个整数值。它是振动的强度。它必须是一个介于1和255之间的值,或者DEFAULT_AMPLITUDE是-1。
您可以将其用作VibrationEffect.DEFAULT_AMPLITUDE
更多详情here

hrysbysz

hrysbysz3#

您可以将其用于触觉反馈(振动):

view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);

HapticFeedbackConstants中还有其他常量可用,如VIRTUAL_KEYKEYBOARD_TAP ...

pepwfjgg

pepwfjgg4#

更新Kotlin

// Vibrates the device for 100 milliseconds.
fun vibrateDevice(context: Context) {
    val vibrator = getSystemService(context, Vibrator::class.java)
    vibrator?.let {
        if (Build.VERSION.SDK_INT >= 26) {
            it.vibrate(VibrationEffect.createOneShot(100, VibrationEffect.DEFAULT_AMPLITUDE))
        } else {
            @Suppress("DEPRECATION")
            it.vibrate(100)
        }
    }
}

调用函数如下:

vibrateDevice(requireContext())

请确保按如下方式向AndroidManifest.xml添加权限:

<uses-permission android:name="android.permission.VIBRATE"/>

请注意,您不需要在运行时请求使用振动的权限。
您需要取消else子句中的弃用,因为警告来自较新的SDK。

1hdlvixo

1hdlvixo5#

我只是偶然发现了这一点,发现VibrationEffect.createWaveform()基本上使用了与旧的vibrate()相同的long[]-模式。
因此,你可以像这样重用你现有的模式(这是一个Kotlin扩展函数):

fun Context.vibrate(pattern: LongArray) {
    val 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)
        )

    } else {
        @Suppress("DEPRECATION")
        vibrator.vibrate(pattern, -1)
    }
}

您也可以使用模式来代替VibrationEffect.createOneShot()(例如longArrayOf(0, 150)),因此无需使用不同的函数。

slsn1g29

slsn1g296#

此库可以帮助您:https://github.com/josephrubin/Rumble-4-Android
您只需要Rumble.once(150);
它为您处理API版本。

1l5u6lss

1l5u6lss7#

为我工作kotlin ext fun
对于触觉效果,振动有5毫秒!!(SHORT_HAPTIC_FEEDBACK_DURATION

fun Context.performHapticFeedback() {
    val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val vibrationEffect = VibrationEffect.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

用法

addOnItemTouchListener(ItemTouchListener { position, event ->
                if (event.action == MotionEvent.ACTION_DOWN) {
                    context.performHapticFeedback()  
                }  
            })

许可

<uses-permission android:name="android.permission.VIBRATE"/>

祝你好运:))

ffx8fchx

ffx8fchx8#


Open管理NuGet软件包
搜索并安装Xamarin.Essentials

try {
 var duration = TimeSpan.FromMilliseconds(300);
 Vibration.Vibrate(duration);
} 
 catch (FeatureNotSupportedException ex){} 
 catch (Exception ex){}
f4t66c6m

f4t66c6m9#

@Suppress("DEPRECATION")
    fun setVibrationSettings(context: Context, isVibrationOn: Boolean) {
        val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            val vibrationEffect =
                VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE)
            (context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager).run {
                if (isVibrationOn) {
                    this.defaultVibrator.vibrate(vibrationEffect)
                } else {
                    this.defaultVibrator.cancel()
                }
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val vibrationEffect =
                VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE)
            (context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator).run {
                if (isVibrationOn) {
                    this.vibrate(vibrationEffect)
                } else {
                    this.cancel()
                }
            }
        } else {
            (context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator).run {
                if (isVibrationOn) {
                    this.vibrate(1000)
                } else {
                    this.cancel()
                }
            }
        }
    }

31和26 SDK的检查SDK代码
ADD权限

<uses-permission android:name="android.permission.VIBRATE"/>

相关问题