关于 PreferenceActivity 的 Android 已弃用方法警告 [英] Android- deprecated method warning regarding PreferenceActivity

查看:90
本文介绍了关于 PreferenceActivity 的 Android 已弃用方法警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试按照 Android 的开发者指南和教程使用首选项创建设置活动时,我收到如下警告:

When I attempt to follow Android's Developer guides and tutorials for creating a Settings Activity using Preferences, I receive warnings such as:

不推荐使用来自 PreferenceActivity 类型的 addPreferencesFromResource(int) 方法"

"The method addPreferencesFromResource(int) from the type PreferenceActivity is deprecated"

对于代码中的这两行:

getPreferenceManager().setSharedPreferencesName(PREFS_NAME);
addPreferencesFromResource(R.xml.default_values);

我知道这些只是警告,但我想知道当我运行我正在设计的应用程序时,它们现在或将来是否会导致任何问题.

I know these are just warnings, but I was wondering if they will cause any issues, now or in the future, when I am running the application that I am designing.

public class DefaultValues extends PreferenceActivity {

    static final String PREFS_NAME = "defaults";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getPrefs(this);
        getPreferenceManager().setSharedPreferencesName(PREFS_NAME);
        addPreferencesFromResource(R.xml.default_values);
    }

    static SharedPreferences getPrefs(Context context) {
        PreferenceManager.setDefaultValues(context, PREFS_NAME, MODE_PRIVATE,
                R.xml.default_values, false);
        return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    }
}

推荐答案

由于该方法已被弃用,建议您不要在代码中使用它,因为它完全有可能在以后的版本中被删除安卓的.但是,我还没有遇到过实际上已从 Android 中删除的弃用方法.

As the method is deprecated, it is recommended that you don't use it in your code, as it is entirely possible that it can be removed in future versions of Android. However, I am yet to come across a deprecated method that has actually been removed from Android.

方法描述中没有提供替代方法,因为首选方法(从 API 级别 11 开始)是实例化 PreferenceFragment 对象从资源文件加载您的首选项.请参阅此处的示例代码:PreferenceActivity

No alternative method is provided in the method's description because the preferred approach (as of API level 11) is to instantiate PreferenceFragment objects to load your preferences from a resource file. See the sample code here: PreferenceActivity

这篇关于关于 PreferenceActivity 的 Android 已弃用方法警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆