【问题标题】:'VIBRATOR_SERVICE: String' is deprecated for API 31API 31 不推荐使用“VIBRATOR_SERVICE: String”
【发布时间】:2021-09-28 15:04:52
【问题描述】:

正如标题所说,我升级到 API 31。我有一个执行振动的功能,但在行

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

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

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

【问题讨论】:

    标签: android deprecated vibration android-api-31


    【解决方案1】:

    The docs for this field 这样说:

    此常量已在 API 级别 31 中弃用。 使用VibratorManager 检索默认系统振动器。

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

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

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

    【讨论】:

    • 谢谢,我确实检查了文档,但不清楚如何使用 VibratorManager,所以我很困惑,决定在这里问它,因为没有类似的问题。基本上,请原谅我的愚蠢,我需要一个例子:)
    【解决方案2】:
    val vib  = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
               val vibratorManager =  this.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
                        vibratorManager.defaultVibrator;
                    } else {
                        getSystemService(VIBRATOR_SERVICE) as Vibrator
                    }
    

    【讨论】:

      【解决方案3】:

      我的答案是在 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 level 30 以下,因此要在 API level 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);
           }
      

      【讨论】:

      • VIBRATOR_SERVICE 已弃用。
      • 哦,是的,我刚才又看了一遍标题。我不知道 Android 12 已弃用 VIBRATOR_SERVICE。我想我会更新我自己的代码来使用 VIBRATOR_MANAGER_SERVICE。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      相关资源
      最近更新 更多