Saving Key-Value Sets 保存键值集
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.
This class shows you how to use the SharedPreferences
APIs to store and retrieve simple values.
这节课向您展示如何使用SharedPreferences 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 theSettings guide.
注意:SharedPreferences APIs键值仅供阅读和写作,你不应该混淆他们的Preference APIs,它帮助您构建一个用户界面应用程序设置(尽管他们使用SharedPreferences作为实现保存应用程序设置)。使用Preference APIs的信息,请参见设置指南。
Get a Handle to a 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.- 使用这个如果你只需要从一个活动使用一个共享的优先文件活动。因为这个检索默认的共享文件,属于活动的优先,你不需要提供一个名称。
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.
例如,下面的代码是在一个片段执行。它访问的共享参数文件确定的资源字符串R.string.preference_file_key并打开它使用私营模式文件只可以访问应用程序。
Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);
When naming your shared preference files, you should use a name that's uniquely identifiable to your app, such as"com.example.myapp.PREFERENCE_FILE_KEY"
Alternatively, if you need just one shared preference file for your activity, you can use thegetPreferences()
method:
共享优先文件命名时,你应该使用一个名称,应用程序的唯一标识,如“com.example.myapp.PREFERENCE_FILE_KEY”或者,如果您只需要一个共同的优先文件为您的活动,您可以使用getPreferences()方法:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution: If you create a shared preferences file withMODE_WORLD_READABLE
orMODE_WORLD_WRITEABLE
, then any other apps that know the file identifier can access your data.
警告:如果你创建一个共享MODE_WORLD_READABLE或MODE_WORLD_WRITEABLE首选项文件,然后知道文件标识符的任何其他应用程序可以访问您的数据。
Write to Shared Preferences
To write to a shared preferences file, create aSharedPreferences.Editor
by calling
edit()
on your
SharedPreferences
.
写信给一个共享的首选项文件,创建一个SharedPreferences。编辑器通过调用编辑SharedPreferences()。
Pass the keys and values you want to write with methods such as putInt()
and putString()
. Then callcommit()
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 asgetInt()
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(),提供你想要的价值的关键,选择返回默认值,如果不存在的关键。例如:
int defaultValue = getResources (). getInteger ( R . string . saved_high_score_default );
long highScore = sharedPref . getInt ( getString ( R . string . saved_high_score ), defaultValue );