混乱与Android的共享首选项-使用哪个功能? [英] Mess with the shared preferences of android - which function to use?

查看:73
本文介绍了混乱与Android的共享首选项-使用哪个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一项标准任务:将一些值存储在应用程序的共享首选项中,以便以后可以检索它. 但是会发现有3种函数可以在其中存储值:

Here's a standard task: store some values in the application's shared preferences in order to be able to retrieve it later on. But one will discover that there are 3 functions to store a value in there:

//1.
public static SharedPreferences PreferenceManager.getDefaultSharedPreferences(Context context) {}

//2.
public SharedPreferences Activity.getPreferences(int mode) {}

//3.
public SharedPreferences ContextWrapper.getSharedPreferences(String name, int mode) {}

因此,现在有一个问题:选择哪个,哪个更好,或者每个都有不同的目的?

So now here's the question: which one to choose and which one is better or there's a different purpose for each of them?

推荐答案

这是我自己的问题的答案:

Here's the answer to my own question:

首先,让我们看一下这三个函数的实现.

First, let's take a look at the implementations of those 3 functions.

//1.
public static SharedPreferences PreferenceManager.getDefaultSharedPreferences(Context context) {
    return context.getSharedPreferences(getDefaultSharedPreferencesName(context), getDefaultSharedPreferencesMode());
}

//2.
public SharedPreferences Activity.getPreferences(int mode) {
    return getSharedPreferences(getLocalClassName(), mode);
}

//3.
public SharedPreferences ContextWrapper.getSharedPreferences(String name, int mode) {
    return mBase.getSharedPreferences(name, mode);
}

此处mBase是对Context类型的对象的引用.

Here mBase is a reference to an object of type Context.

我们看到第二个函数调用了第三个函数,所有这三个函数基本上是等效的,但参数不同.想想超载.

We see that the 2nd functions calls the 3rd one, and all those 3 functions are basically equivalent but with different parameters. Think of overloading.

接下来,深入了解第一个函数的实现,我们可以将其调用简化如下:

Next, going deeper to the 1st function's implementation, we can simplify its call as the following:

//1.
public static SharedPreferences PreferenceManager.getDefaultSharedPreferences(Context context) {
    return context.getSharedPreferences(context.getPackageName() +
        "_preferences", Context.MODE_PRIVATE);
}

,同样,对于第二个功能:

and similarly, for the second function:

//2.
public SharedPreferences Activity.getPreferences(int mode) {
    return mBase.getSharedPreferences(getLocalClassName(), mode);
}

总而言之,第一个函数创建一个名为<your_package_name>_preferences的共享首选项文件,第二个函数创建一个名为<your_class_name>的共享首选项文件,最后,第三个函数让您指定共享首选项文件的任意名称.

To sum-up, the 1st function creates a shared preference file with a name as <your_package_name>_preferences, the 2nd function creates a shared preference file with a name as <your_class_name>, and finally, the 3rd function lets you to specify arbitrary name for the shared preference file.

不用说,您需要为共享首选项文件指定正确的名称,以便取回保存的值.因此,您可以使用第3个函数自己指定名称,也可以分别使用第1个或第2个函数来保存名称.

Needless to say that you need to specify the correct name for the shared preference file in order to retrieve the saved values back. So you may use the 3rd function to specify the name yourself or to use the 1st or 2nd function respective to how you have saved it before.

警告!确保传递正确的Context类实例. 例如,一个混乱的情况看起来像这样:您正在从系统中运行的后台线程保存到共享的首选项中(例如,当使用android的现成的SyncAdapter框架时),并试图找回从UI线程保存的值,您可能会得到默认/错误值!

Warning! Make sure you are passing the correct instance of the Context class. For example, a messy scenario would look like this: you are saving into shared preferences from a background thread which is running in the system (like for example when using the android's out-of-the-box SyncAdapter framework) and trying to get back the saved values from your UI-thread, you may get the default/wrong values!

希望这对其他人有帮助...;)

Hope this will be helpful for someone else... ;)

这篇关于混乱与Android的共享首选项-使用哪个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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