最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android本地存儲SharedPreferences詳解

 更新時間:2017年05月28日 10:07:59   作者:claireyuancy  
這篇文章主要介紹了Android本地存儲SharedPreferences詳解的相關(guān)資料,需要的朋友可以參考下

Android本地存儲SharedPreferences詳解

存儲位置

SharedPreferences數(shù)據(jù)保存在: /data /data/<package_name> /shared_prefs 文件夾下,以XML格式保存,根元素為:<map />。文件名稱為獲取SharedPreferences實例時傳遞的參數(shù)值。

<map>
<int name="key" value="value" />
<string name="key" > value </string>
</map>

獲取實例

SharePerferences是一個接口,Context實現(xiàn)了API:getSharedPreferences(String, int);這個API依據(jù)String返回一個SharePerferences實例。同樣的String返回的SharePerferences的實例是同樣的。此處strign指定的是存儲數(shù)據(jù)的xml文件的名稱。

Activity實現(xiàn)了getPreferences(int),這種方法就是默認(rèn)使用Activity的class name作為String調(diào)用 getSharedPreferences(String, int)。

//指定該SharedPreferences數(shù)據(jù)僅僅能被本應(yīng)用讀、寫。

Context.MODE_PRIVATE 
//指定該SharedPreferences數(shù)據(jù)也能被其它應(yīng)用程序讀,但不能寫。
Context.MODE_WORLD_READABLE 
//指定該SharedPreferences數(shù)據(jù)也能被其它應(yīng)用程序讀、寫。
Context.MODE_WORLD_WRITEABLE
//文件是否存在,存在那么追加,否則新建
Context.MODE_WORLD_APPEND

//推斷SharedPreferences是否包括特定key的數(shù)據(jù)。
boolean contains(String key) 
//獲取SharedPreferences里所有的key-value對。
Map<String,?> getAll() 
//獲取指定key相應(yīng)的value,假設(shè)key不存在。則返回默認(rèn)值defValue。

Xxx getXxx(String key, Xxx defValue) 



SharedPreferences接口本身未提供寫入數(shù)據(jù)的能力,而是通過其內(nèi)部接口。其調(diào)用SharedPreferences.edit() 方法就可以獲得它所相應(yīng)的 SharedPreferences.Editor對象。Editor有例如以下向SharedPreferences寫入數(shù)據(jù)的方法:


//清空SharedPreferences里全部數(shù)據(jù)。
Editor.clear() 
//向SharedPreferences里存入指定key相應(yīng)的數(shù)據(jù)。

Editor.putXxx(String key, Xxx Value) 
//刪除SharedPreferences里指定key相應(yīng)的數(shù)據(jù)項。


Editor.remove(String key)
//當(dāng)Editor編輯完畢后,調(diào)用該方法提交改動。 
boolean Editor.commit()



讀寫其它應(yīng)用的SharedPreferences

1.創(chuàng)建其它應(yīng)用的相應(yīng) Context。

Context useContext =createPackageContext("package_name",Context.CONTEXT_IGNORE_SECURITY);
2.調(diào)用其它應(yīng)用的 Context的 getSharedPreferences()方法獲取 SharedPreferences對象。

3.調(diào)用其它應(yīng)用的 SharedPreferences.edit()方法獲得對應(yīng)SharedPreferences.Editor對象。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:

相關(guān)文章

最新評論

博湖县| 济源市| 桃园市| 科尔| 南昌县| 饶河县| 辽阳市| 漯河市| 南城县| 浙江省| 济源市| 木兰县| 岱山县| 普兰县| 高州市| 连州市| 张家港市| 邹平县| 大同市| 察雅县| 罗田县| 屯留县| 石景山区| 南木林县| 沧州市| 云安县| 奇台县| 孟津县| 中西区| 辽阳县| 泽州县| 普宁市| 岳普湖县| 滨州市| 都兰县| 同江市| 汝城县| 彭阳县| 乌拉特中旗| 密云县| 宜春市|