Android haptic feedback is not working in Samsung

Issue

I’m integrating haptic feedback in my android app which works fine in my Google Pixel 4a phone but its not working at all in Samsung devices. I’m using below snippet for calling it after getting a result from an API call:

activity?.window?.decorView?.performHapticFeedback(HapticFeedbackConstants.CONFIRM, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING)

I have defined permission for Vibrate also in AndroidManifest.xml as:

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

Please let me know if it’s not possible without any click or touch listener.

Solution

I didn’t find the solution with haptic feedback for Samsung devices so I went with the android’s default vibration. Find below the snippet:

    if (Build.VERSION.SDK_INT >= 26) {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(VibrationEffect.createOneShot(250, VibrationEffect.EFFECT_DOUBLE_CLICK))
    } else {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(250)
    }

Answered By – Anshul Tyagi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published