【问题标题】:What use instead of deprecated android.preference library on android 10+?在 android 10+ 上使用什么来代替弃用的 android.preference 库?
【发布时间】:2020-01-11 22:31:56
【问题描述】:

我启动了新的电视应用程序,现在我需要为设置的流质量、流语言等开发动态偏好。 所以我打算使用 android.preference 库并根据流数据动态生成 perfs。但是在https://developer.android.com/guide/topics/ui/settings.html 上是

注意:本指南介绍了如何使用 AndroidX Preference 库。 从 Android 10 开始,平台 android.preference 库为 已弃用。

但是没有重定向到替换。那么用什么来代替 android.preference 库呢?

该指南上也有说明:

注意:本指南假设您正在使用 androidx.preference:preference:1.1.0-alpha04 或更高版本。一些功能 可能在旧版本的库上不可用。

所以androidx.preference:preference 将被弃用。

【问题讨论】:

    标签: android android-preferences androidx


    【解决方案1】:

    澄清升级以使用 AndroidX Preference 库的步骤(基于@GabrieleMariotti 的有用回答):

    • 在您的build.gradle (Module:app) 中添加以下内容到dependencies

      dependencies {
      ...
      implementation 'androidx.appcompat:appcompat:1.1.0'
      implementation "androidx.preference:preference:1.1.0"
      ...
      }
      
    • 我还在 res/xml/my_preferences.xml 中保留了 xmlns:android url,尽管 documentation 另有建议。也就是说,我有

      <PreferenceScreen
          xmlns:android="http://schemas.android.com/apk/res/android">
      
          <PreferenceCategory android:title="Individual">
              <Preference
                  android:key="preference_play_activity"
                  android:title="Play Activity">
                  <intent
                      android:targetClass="com.myorg.mythirdapp.PlayActivity"
                      android:targetPackage="com.myorg.mythirdapp" />
              </Preference>
              ...
      

      ...而不是...

      <PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
      

    【讨论】:

      【解决方案2】:

      平台 android.preference 库已弃用。但是没有重定向到替换。

      您可以签入Android 10 documentation
      android.preference 库自 Android 10 起已弃用。开发人员应改为使用 AndroidX 首选项库,它是 Android Jetpack 的一部分。

      替换为 androidx-preference 库。
      只需使用:

      dependencies {
          def preference_version = "1.1.0"
      
          // Java
          implementation "androidx.preference:preference:$preference_version"
          // Kotlin
          implementation "androidx.preference:preference-ktx:$preference_version"
      }
      

      更多关于release notes的信息。

      还有:

      注意:本指南假设您使用的是 androidx.preference:preference:1.1.0-alpha04 或更高版本。某些功能在旧版本的库上可能不可用。

      It means 表示,虽然版本处于 alpha 阶段,但可能会添加、删除或更改 API。

      【讨论】:

      • 您所指的内容似乎将被弃用。查看更新的问题。
      • @LunaVulpo 有误会。 Androidx-preference 库是相当新的,它将取代 android 首选项。您可以查看 old android.preference 平台的文档。 这些类已被弃用。使用 AndroidX Preference Library 在所有设备上实现一致的行为。 此外,稳定版 1.1.0 已发布few days ago
      • 您也可以在 Android 10 changes 中查看此链接。我刚刚更新了答案。
      • AndroidX 变体目前很可能不会被弃用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多