Saving Key-Value Sets
保存键值集合
If you have a relatively small collection of key-values that you'd like to save, you should use the
SharedPreferences
APIs. A
SharedPreferences
object points to a file containing key-value pairs and provides simple methods to read and write them. Each
SharedPreferences
file is managed by the framework and can be private or shared.
如果你有一个相对小的键值集合需要保存,你应该使用SharedPreferences APIs。一个SharedPreferences 对象指向一个包含键值对的文件并且提供简单的方法去读写。
每个SharedPreferences 文件是通过框架去管理的并且可以是私有的或者是共享的。
这节课教你如何使用SharePreferences APIs 去存储和检索简单值。
Note:
The
SharedPreferences
APIs are only for reading and writing key-value pairs and you should not confuse them with the
Preference
APIs, which help you build a user interface for your app settings (although they use
SharedPreferences
as their implementation to save the app settings). For information about using the
Preference
APIs, see the
Settings
guide.
注意:SharedPreferences APIs 只是读写键值对并且你不应该和Preference APIs混淆,Preference APIs 是用于构建app设置的UI(虽然它们使用SharedPreferences去保存app 设置)。关于使用Preference APIs的详情,请看Settings指导。
Get a Handle to a SharedPreferences
获取一个SharedPreferences的句柄
You can create a new shared preference file or access an existing one by calling one of two methods:
创建一个新的首选项文件或访问一个已存在的可以通过调用下列两种方法之一:
getSharedPreferences()
— Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from anyContext
in your app.getPreferences()
— Use this from anActivity
if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.
getSharedPreferences() — 如果你需要多个通过文件名标识的首选项文件就使用这个方法,名称可以通过指定这个方法的一个参数进行设置。你可以在app的任何Context中调用这个方法。
getPreferences() — 如果你的Activity只需要一个首选项文件就使用这个方法在Activity中。因为这个方法是从activity中检索一个默认的首选项文件,所以你不需要指定文件名
For example, the following code is executed inside a
Fragment
. It accesses the shared preferences file that's identified by the resource string R.string.preference_file_key
and opens it using the private mode so the file is accessible by only your app.
例如,下面的代码在Fragment中执行。它去访问通过资源字符串指定名R.string.preference_file_key的首选项文件并且使用私有模式打开所以这个文件只能被你自己的app访问。
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
"com.example.myapp.PREFERENCE_FILE_KEY"
当你命名首选项文件的时,你应该使用一个在你的app里是唯一的文件名,例如 “com.example.myapp.PREFERENCE_KEY”
Alternatively, if you need just one shared preference file for your activity, you can use the
getPreferences()
method:
或者,在你的activity你仅是需要一个首选项文件,那你可以使用getPreferences()方法:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution: If you create a shared preferences file with MODE_WORLD_READABLE
or MODE_WORLD_WRITEABLE
, then any other apps that know the file identifier can access your data.
Write to Shared Preferences
写内容
To write to a shared preferences file, create a
SharedPreferences.Editor
by calling
edit()
on your
SharedPreferences
.
写内容到一个首选项文件是,你可以调用SharedPreferences的edit() 方法创建一个SaredPreferences.Editor
Pass the keys and values you want to write with methods such as
putInt()
and
putString()
. Then call
commit()
to save the changes. For example:
键值的写入是调用类似putInt()和putString()这样的方法。然后调用commit()方法保存数据。例如:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
Read from Shared Preferences
读取内容
To retrieve values from a shared preferences file, call methods such as
getInt()
and
getString()
, providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:
从首选项文件检索值,可以调用类似getInt()和getString()这样的方法,使用值相应的键就可以了,并且可以返回一个默认值。例如:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);