[Andriod官方训练教程]保存数据之保存键-值对的集合
2013-02-04 18:42
501 查看
原文地址:https://developer.android.com/training/basics/data-storage/shared-preferences.html
-------------------------------------------------------------------------------------------------------------------------------
If you have a relatively small collection of key-values that you'd like to save, you should use the
object points to a file containing key-value pairs and provides simple methods to read and write them. Each
and can be private or shared.
如果你有一个相对较小的键-值对集合想要存储,你应该使用SharedPreferences APIs。一个SharedPreferences对象指向一个包含键值对的文件,并且提供了一些简单的方法来进行读取和写入它们。每个SharedPreferences文件可以是私有的或是共享的,靠framework来管理它们。
This class shows you how to use the
这节课演示如何使用SharedPreferences APIs来存储和检索简单的值。
Note: The
the
as their implementation to save the app settings). For information about using the
guide.
注意:SharedPreferences APIs不仅仅可以读取和写入键值对,而且你不应该把它们和Preference
APIs相混淆,后者帮助你为你的app设置建立一个用户接口(尽管在存储app设置时它们使用SharedPreferences实现)。
with the first parameter. You can call this from any
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的默认共享应用文件,你不需要提供一个名称。
For example, the following code is executed inside a
and opens it using the private mode so the file is accessible by only your app.
例如,下面的代码在一个Fragment中执行。它依靠资源字符R.string.preference_file_key定义来访问一个共享应用文件,然后使用私有模式打开它,以使文件仅仅对你的app是可访问的。
When naming your shared preference files, you should use a name that's uniquely identifiable to your app, such as
Alternatively, if you need just one shared preference file for your activity, you can use the
当给你的共享引用文件命名时,你应该使用一个唯一的、可确认的名字,例如“com.example.myapp.PREFERENCE_FILE_KEY”。或者,如果你仅仅需要一个共享引用文件,你可以使用getPreferences()方法。
Caution: If you create a shared preferences file with
then any other apps that know the file identifier can access your data.
警告:如果你使用
,那么其他任何apps都知道这个文件标识符可以访问你的数据。
on your
为了向一个共享的应用文件写入,通过在你的SharedPreferences调用
Pass the keys and values you want to write with methods such as
Then call
利用
providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:
为了从一个共享引用文件中检索数值,调用getInt() and
-------------------------------------------------------------------------------------------------------------------------------
If you have a relatively small collection of key-values that you'd like to save, you should use the
SharedPreferencesAPIs. A
SharedPreferences
object points to a file containing key-value pairs and provides simple methods to read and write them. Each
SharedPreferencesfile is managed by the framework
and can be private or shared.
如果你有一个相对较小的键-值对集合想要存储,你应该使用SharedPreferences APIs。一个SharedPreferences对象指向一个包含键值对的文件,并且提供了一些简单的方法来进行读取和写入它们。每个SharedPreferences文件可以是私有的或是共享的,靠framework来管理它们。
This class shows you how to use the
SharedPreferencesAPIs to store and retrieve simple values.
这节课演示如何使用SharedPreferences APIs来存储和检索简单的值。
Note: The
SharedPreferencesAPIs are only for reading and writing key-value pairs and you should not confuse them with
the
PreferenceAPIs, 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
PreferenceAPIs, see theSettings
guide.
注意:SharedPreferences APIs不仅仅可以读取和写入键值对,而且你不应该把它们和Preference
APIs相混淆,后者帮助你为你的app设置建立一个用户接口(尽管在存储app设置时它们使用SharedPreferences实现)。
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 any
Contextin your app.
getPreferences()— Use this from an
Activityif
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的默认共享应用文件,你不需要提供一个名称。
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);
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 the
getPreferences()method:
当给你的共享引用文件命名时,你应该使用一个唯一的、可确认的名字,例如“com.example.myapp.PREFERENCE_FILE_KEY”。或者,如果你仅仅需要一个共享引用文件,你可以使用getPreferences()方法。
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution: If you create a shared preferences file with
MODE_WORLD_READABLEor
MODE_WORLD_WRITEABLE,
then any other apps that know the file identifier can access your data.
警告:如果你使用
MODE_WORLD_READABLE或者
MODE_WORLD_WRITEABLE
,那么其他任何apps都知道这个文件标识符可以访问你的数据。
Write to Shared Preferences —— 向共享引用写入数据
To write to a shared preferences file, create aSharedPreferences.Editorby calling
edit()
on your
SharedPreferences.
为了向一个共享的应用文件写入,通过在你的SharedPreferences调用
edit()来创建一个SharedPreferences.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 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() and
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);
相关文章推荐
- [Andriod官方训练教程]保存数据之保存键-值对的集合
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个灵活的UI
- [Andriod官方训练教程]支持不同的设备之支持不同的语言
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个灵活的UI
- [Andriod官方训练教程]支持不同的设备之支持不同的语言
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之创建一个Fragment
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之与其他Fragments进行交互
- [Andriod官方训练教程]创建你的第一个App之开始另一个Activity
- [Andriod官方训练教程]管理Activity的生命活动之重新创建一个Activity
- [Andriod官方训练教程]使用Fragment创建一个动态的UI之与其他Fragments进行交互
- [Andriod官方训练教程]创建你的第一个App之开始另一个Activity
- [Andriod官方训练教程]管理Activity的生命活动之暂停和恢复一个Activity
- [Andriod官方训练教程]管理Activity的生命活动之重新创建一个Activity
- [Andriod官方训练教程]管理Activity的生命活动之暂停和恢复一个Activity
- [Andriod官方训练教程]支持不同的设备之支持不同的屏幕
- [Andriod官方训练教程]管理Activity的生命活动之停止和重启一个Activity
- [Andriod官方训练教程]支持不同的设备之支持不同的屏幕
- [Andriod官方训练教程]创建你的第一个App之创建一个Android项目
- [Andriod官方训练教程]创建你的第一个App之创建一个Android项目