'VIBRATOR_SERVICE: String' 已弃用 API 31

'VIBRATOR_SERVICE: String' is deprecated for API 31

正如标题所说,我升级到API 31.我有一个振动功能,但是在线

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

VIBRATOR_SERVICE 现在显示为已弃用。我该如何更换它?或者至少,API 31 岁及以上的现代解决方案是什么?

编辑:正如 Joachim Sauer 所写,替代方案是 VibrationManager。我现在需要的是使用 VibrationManager 的等效代码行。

The docs for this field这样说:

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

需要 Vibrator 实例的代码最直接的翻译是这样的:

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

一般来说,当 class/method/field 像这样被弃用时,您应该首先查看文档。几乎每次它都会告诉你用什么代替(或者在某些情况下它没有替代品)。

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
}

我的答案在Java。如果你能看懂的话。此代码适用于新旧 android 设备。参考文档 Vibrate constantly for the specified period of time.。您应该使用 VibrationEffect 来创建振动模式。

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

编辑

此方法适用于 API 30 级以下,因此要在 API 31 级以上完全使用此方法,您需要使用 VIBRATOR_MANAGER_SERVICE 而不是 VIBRATOR_SERVICE,检索默认振动器服务。

正确代码如下:

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

这是对新旧都简单的回答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);
    }