'VIBRATOR_SERVICE: String' is deprecated for API 31

Viewed 7512

As the title says, i upgraded to API 31. I had a function to perform a vibration, but in the line

val vib = this.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator

the VIBRATOR_SERVICE is now shown as deprecated. How can i replace it? Or at least, what's the modern solution for API 31 and above?

EDIT: as Joachim Sauer wrote, the alternative is VibrationManager. What i need now is the equivalent line of code using VibrationManager.

4 Answers
val vib = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    val vibratorManager =
        getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
    vibratorManager.defaultVibrator
} else {
    @Suppress("DEPRECATION")
    getSystemService(VIBRATOR_SERVICE) as Vibrator
}

The docs for this field say this:

This constant was deprecated in API level 31. Use VibratorManager to retrieve the default system vibrator.

The most direct translation of code needing a Vibrator instance would be this:

val vibratorManager = this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val vibrator = vibratorManager.getDefaultVibrator();

Generally speaking whenever a class/method/field is deprecated like this then you should first check the documentation. Almost every single time it will tell you what to use instead (or in some cases that it has no replacement).

My Answer is in Java. If you can understand it. This code works for both old and new android devices. Reference to the docs Vibrate constantly for the specified period of time.. You should use a VibrationEffect instead to create the vibration pattern.

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

final int DELAY = 0, VIBRATE = 1000, SLEEP = 1000, START = 0;
long[] vibratePattern = {DELAY, VIBRATE, SLEEP};
    
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

   vibrator.vibrate(VibrationEffect.createWaveform(vibratePattern, START));

   } else {
        // backward compatibility for Android API < 26
        // noinspection deprecation
        vibrator.vibrate(vibratePattern, START);
   }

Edit

This method works for API level 30 below properly, so to completely use this on API level 31 above you need to use VIBRATOR_MANAGER_SERVICE instead of VIBRATOR_SERVICE, to retrieve the default vibrator service.

The correct code is below:

Vibrator vibrator;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {

   VibratorManager vibratorManager = (VibratorManager) getSystemService(Context.VIBRATOR_MANAGER_SERVICE);

   vibrator = vibratorManager.getDefaultVibrator();

  } else {
        // backward compatibility for Android API < 31,
        // VibratorManager was only added on API level 31 release.
        // noinspection deprecation
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

  }

  final int DELAY = 0, VIBRATE = 1000, SLEEP = 1000, START = 0;
  long[] vibratePattern = {DELAY, VIBRATE, SLEEP};
    
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

     vibrator.vibrate(VibrationEffect.createWaveform(vibratePattern, START));

     } else {
          // backward compatibility for Android API < 26
          // noinspection deprecation
          vibrator.vibrate(vibratePattern, START);
     }

this is simple answer for both old and new api

private void vibrate() {
    //vibrate phone
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        VibratorManager vibratorManager = (VibratorManager) mContext.getSystemService(Context.VIBRATOR_MANAGER_SERVICE);
        vibratorManager.getDefaultVibrator();
    }
    else {
        Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(10);
    }
Related