How can I check if haptic feedback enabled in system on Android 13 or higher?

757 views Asked by At

I'm trying to get if haptic feedback enabled in System Settings, i.e. globally on a device. I don't need to set this setting, just read.

We do it this way on Android 12 or lower:

Settings.System.getInt(context.contentResolver, Settings.System.HAPTIC_FEEDBACK_ENABLED) == 1

But with the release of Android 13 Google deprecated flag HAPTIC_FEEDBACK_ENABLED. Now it recommended to use android.os.VibrationAttributes.USAGE_TOUCH. It's really incomprehensible how to use it.

I need something like this:

 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
     }
1

There are 1 answers

2
Gustavo Ross On

In the new Android Version (13 or higher) you can use VibrationManager to check this, please check below:

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
        }
    }