我试图了解是否在系统设置中启用了触觉反馈,即在设备上全局启用。我不需要设置这个设置,直接阅读即可。
我们在 Android 12 或更低版本上这样做:
Settings.System.getInt(context.contentResolver, Settings.System.HAPTIC_FEEDBACK_ENABLED) == 1
但是随着 Android 13 的发布,谷歌弃用了标志
HAPTIC_FEEDBACK_ENABLED
。现在推荐使用android.os.VibrationAttributes.USAGE_TOUCH
。
实在是看不懂怎么用
我需要这样的东西:
val hapticEnabled: Boolean
get() = if (BuildConfig.VERSION_CODE >= Build.VERSION_CODES.TIRAMISU) {
// here what I'm trying to find out
} else {
Settings.System.getInt(context.contentResolver, Settings.System.HAPTIC_FEEDBACK_ENABLED) == 1
}
在新的 Android 版本(13 或更高版本)中,您可以使用 VibrationManager 进行检查,请检查以下内容:
val hapticEnabled: Boolean
get() {
return if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val vibratorManager = requireContext()
.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val vibrator = vibratorManager.defaultVibrator
vibrator.hasVibrator()
} else {
Settings.System.getInt(requireContext().contentResolver, Settings.System.HAPTIC_FEEDBACK_ENABLED) == 1
}
}