1、使用sharedpreferences儲存資料方法如下:
//執行個體化sharedpreferences對象(第一步)
sharedpreferences mysharedpreferences= getsharedpreferences("test",
activity.mode_private);
//執行個體化sharedpreferences.editor對象(第二步)
sharedpreferences.editor editor = mysharedpreferences.edit();
//用putstring的方法儲存資料
editor.putstring("name", "karl");
editor.putstring("habit", "sleep");
//送出目前資料
editor.commit();
//使用toast資訊提示框提示成功寫入資料
toast.maketext(this, "資料成功寫入sharedpreferences!" , toast.length_long).show();
2、使用sharedpreferences讀取資料方法如下:
//同樣,在讀取sharedpreferences資料前要執行個體化出一個sharedpreferences對象
sharedpreferencessharedpreferences= getsharedpreferences("test",
// 使用getstring方法獲得value,注意第2個參數是value的預設值
string name =sharedpreferences.getstring("name", "");
string habit =sharedpreferences.getstring("habit", "");
//使用toast資訊提示框顯示資訊
toast.maketext(this, "讀取資料如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,
toast.length_long).show();
3、sharedpreferences的四種操作模式:
context.mode_private
context.mode_append
context.mode_world_readable
context.mode_world_writeable
context.mode_private:為預設操作模式,代表該檔案是私有資料,隻能被應用本身通路,在該模式下,寫入的内容會覆寫原檔案的内容
context.mode_append:模式會檢查檔案是否存在,存在就往檔案追加内容,否則就建立新檔案.
context.mode_world_readable和context.mode_world_writeable用來控制其他應用是否有權限讀寫該檔案.
mode_world_readable:表示目前檔案可以被其他應用讀取.
mode_world_writeable:表示目前檔案可以被其他應用寫入.
很多時候我們開發的軟體需要向使用者提供軟體參數設定功能,例如我們常用的qq,使用者可以設定是否允許陌生人添加自己為好友。對于軟體配置參數的儲存,如果是window軟體通常我們會采用ini檔案進行儲存,如果是j2se應用,我們會采用properties屬性檔案或者xml進行儲存。如果是android應用,我們最适合采用什麼方式儲存軟體配置參數呢?android平台給我們提供了一個sharedpreferences類,它是一個輕量級的存儲類,特别适合用于儲存軟體配置參數。使用sharedpreferences儲存資料,其背後是用xml檔案存放資料,檔案存放在/data/data/<package name>/shared_prefs目錄下:
sharedpreferences sharedpreferences = getsharedpreferences("ljq", context.mode_private);
editor editor = sharedpreferences.edit();//擷取編輯器
editor.putstring("name", "林計欽");
editor.putint("age", 24);
editor.commit();//送出修改
生成的ljq.xml檔案内容如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">林計欽</string>
<int name="age" value="24" />
</map>
因為sharedpreferences背後是使用xml檔案儲存資料,getsharedpreferences(name,mode)方法的第一個參數用于指定該檔案的名稱,名稱不用帶字尾,字尾會由android自動加上。方法的第二個參數指定檔案的操作模式,共有四種操作模式,這四種模式前面介紹使用檔案方式儲存資料時已經講解過。如果希望sharedpreferences背後使用的xml檔案能被其他應用讀和寫,可以指定context.mode_world_readable和context.mode_world_writeable權限。
另外activity還提供了另一個getpreferences(mode)方法操作sharedpreferences,這個方法預設使用目前類不帶包名的類名作為檔案的名稱。
通路sharedpreferences中的資料
如果通路其他應用中的preference,前提條件是:該preference建立時指定了context.mode_world_readable或者context.mode_world_writeable權限。
如:有個<package name>為com.ljq.action的應用使用下面語句建立了preference。
getsharedpreferences("ljq", context.mode_world_readable);
其他應用要通路上面應用的preference,首先需要建立上面應用的context,然後通過context 通路preference ,通路preference時會在應用所在包下的shared_prefs目錄找到preference :
context otherappscontext = createpackagecontext("com.ljq.action", context.context_ignore_security);
sharedpreferences sharedpreferences = otherappscontext.getsharedpreferences("ljq", context.mode_world_readable);
string name = sharedpreferences.getstring("name", "");
int age = sharedpreferences.getint("age", 0);
如果不通過建立context通路其他應用的preference,也可以以讀取xml檔案方式直接通路其他應用preference對應的xml檔案,如:
file xmlfile = new file("/data/data/<package name>/shared_prefs/itcast.xml");//<package name>應替換成應用的包名
轉載:http://blog.csdn.net/chaoyu168/article/details/50553807