[Andriod官方训练教程]保存数据之保存键-值对的集合
原文地址: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 theSharedPreferences APIs. ASharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. EachSharedPreferences file is managed by the framework and can be private or shared.
如果你有一個(gè)相對(duì)較小的鍵-值對(duì)集合想要存儲(chǔ),你應(yīng)該使用SharedPreferences?APIs。一個(gè)SharedPreferences對(duì)象指向一個(gè)包含鍵值對(duì)的文件,并且提供了一些簡(jiǎn)單的方法來(lái)進(jìn)行讀取和寫(xiě)入它們。每個(gè)SharedPreferences文件可以是私有的或是共享的,靠framework來(lái)管理它們。
This class shows you how to use the SharedPreferences APIs to store and retrieve simple values.
這節(jié)課演示如何使用SharedPreferences?APIs來(lái)存儲(chǔ)和檢索簡(jiǎn)單的值。
Note: The SharedPreferences APIs are only for reading and writing key-value pairs and you should not confuse them with thePreference APIs, which help you build a user interface for your app settings (although they useSharedPreferences as their implementation to save the app settings). For information about using thePreference APIs, see theSettings guide.
注意:SharedPreferences?APIs不僅僅可以讀取和寫(xiě)入鍵值對(duì),而且你不應(yīng)該把它們和Preference APIs相混淆,后者幫助你為你的app設(shè)置建立一個(gè)用戶接口(盡管在存儲(chǔ)app設(shè)置時(shí)它們使用SharedPreferences實(shí)現(xiàn))。
?
Get a Handle to a SharedPreferences ——得到一個(gè)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.
你可以通過(guò)調(diào)用下面兩個(gè)方法之一創(chuàng)建一個(gè)新的共享引用文件或進(jìn)入一個(gè)已經(jīng)存在的文件:
- getSharedPreferences()——如果你需要多個(gè) 依靠名字(第一個(gè)參數(shù))定義的共享引用文件,你可以使用這個(gè)函數(shù)。你可以從你的app中的任意Context里調(diào)用它。
- getPreferences()——如果你在這個(gè)activity中僅僅需要使用一個(gè)共享應(yīng)用文件,就使用這個(gè)方法。因?yàn)樗鼨z索一個(gè)屬于這個(gè)activity的默認(rèn)共享應(yīng)用文件,你不需要提供一個(gè)名稱(chēng)。
For example, the following code is executed inside a Fragment. It accesses the shared preferences file that's identified by the resource stringR.string.preference_file_key and opens it using the private mode so the file is accessible by only your app.
例如,下面的代碼在一個(gè)Fragment中執(zhí)行。它依靠資源字符R.string.preference_file_key定義來(lái)訪問(wèn)一個(gè)共享應(yīng)用文件,然后使用私有模式打開(kāi)它,以使文件僅僅對(duì)你的app是可訪問(wèn)的。
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:
當(dāng)給你的共享引用文件命名時(shí),你應(yīng)該使用一個(gè)唯一的、可確認(rèn)的名字,例如“com.example.myapp.PREFERENCE_FILE_KEY”。或者,如果你僅僅需要一個(gè)共享引用文件,你可以使用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 或者M(jìn)ODE_WORLD_WRITEABLE ,那么其他任何apps都知道這個(gè)文件標(biāo)識(shí)符可以訪問(wèn)你的數(shù)據(jù)。
?
Write to Shared Preferences —— 向共享引用寫(xiě)入數(shù)據(jù)
To write to a shared preferences file, create a SharedPreferences.Editor by callingedit() on yourSharedPreferences.
為了向一個(gè)共享的應(yīng)用文件寫(xiě)入,通過(guò)在你的SharedPreferences調(diào)用edit() 來(lái)創(chuàng)建一個(gè)SharedPreferences.Editor。
Pass the keys and values you want to write with methods such as putInt() andputString(). Then callcommit() to save the changes. For example:
利用putInt()?和putString()這樣的方法傳遞你想要寫(xiě)入的鍵和值。然后調(diào)用commit()來(lái)存儲(chǔ)變化。例如:
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 —— 從共享引用中讀取數(shù)據(jù)
To retrieve values from a shared preferences file, call methods such as getInt() andgetString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:
為了從一個(gè)共享引用文件中檢索數(shù)值,調(diào)用getInt() andgetString()等方法,提供一個(gè)你想到得到的鍵,和一個(gè)供選擇的默認(rèn)返回值(如果這個(gè)鍵不存在的話)。例如:
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);?
轉(zhuǎn)載于:https://www.cnblogs.com/xiaowangba/archive/2013/02/04/6314740.html
總結(jié)
以上是生活随笔為你收集整理的[Andriod官方训练教程]保存数据之保存键-值对的集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 启动文件、简单的消息框
- 下一篇: 网络爬虫入门