Android中SharedPreference詳解及簡(jiǎn)單實(shí)例
Android中SharedPreference詳解
SharedPreference是Android提供的一種輕量級(jí)的數(shù)據(jù)存儲(chǔ)方式,主要用來存儲(chǔ)一些簡(jiǎn)單的配置信息,例如,默認(rèn)歡迎語(yǔ),登錄用戶名和密碼等。其以鍵值對(duì)的方式存儲(chǔ),使得我們能很方便進(jìn)行讀取和存入。
SharedPreference 文件保存在/data/data/<package name>/shared_prefs 路徑下(如/data/data/com.android.alarmclock/shared_prefs/com.android.text_preferences.xml),通過cat命令可以查看文件,如:

通過Activity自帶的getSharedPreferences方法,可以得到SharedPreferences對(duì)象。
public abstract SharedPreferences getSharedPreferences (String name, int mode);
name:表示保存后 xml 文件的名稱
mode:表示 xml 文檔的操作權(quán)限模式(私有,可讀,可寫),使用0或者M(jìn)ODE_PRIVATE作為默認(rèn)的操作權(quán)限模式。
1.數(shù)據(jù)讀?。?br />
通過SharedPreferences對(duì)象的鍵key可以獲取到對(duì)應(yīng)key的鍵值。對(duì)于不同類型的鍵值有不同的函數(shù):
getBoolean,getInt,getFloat,getLong. public abstract String getString (String key, String defValue);
2.數(shù)據(jù)存入:
數(shù)據(jù)的存入是通過SharedPreferences對(duì)象的編輯器對(duì)象Editor來實(shí)現(xiàn)的。通過編輯器函數(shù)設(shè)置鍵值,然后調(diào)用commit()提交設(shè)置,寫入xml文件。
public abstract SharedPreferences.Editor edit (); public abstract SharedPreferences.Editor putString (String key, String value); public abstract boolean commit ();
下面一個(gè)實(shí)例顯示一個(gè)TextView,上面顯示用戶使用該應(yīng)用的次數(shù)。
效果圖如下:

源代碼如下:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
TestSharedPreferences.java:
package com.android.test;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class TestSharedPreferences extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences mSharedPreferences = getSharedPreferences("TestSharedPreferences", 0);
// SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int counter = mSharedPreferences.getInt("counter", 0);
TextView mTextView = (TextView)findViewById(R.id.textview);
mTextView.setText("This app has been started " + counter + " times.");
SharedPreferences.Editor mEditor = mSharedPreferences.edit();
mEditor.putInt("counter", ++counter);
mEditor.commit();
}
}
幾點(diǎn)說明:
1.SharedPreferences的獲取有兩種方法:
一是上面提到的通過Activity自帶(本質(zhì)來講是Context的)的getSharedPreferences方法,可以得到SharedPreferences對(duì)象。這種方法的好處是可以指定保存的xml文件名。
另一種是通過PreferenceManager.getSharedPreferences(Context)獲取SharedPreferences對(duì)象。這種方法不能指定保存的xml文件名,文件名使用默認(rèn)的:<package name>+"_preferences.xml"的形式,不過如果在一個(gè)包里面采用這種方式需要保存多個(gè)這樣的xml文件,可能會(huì)亂掉。建議采用第一種指定xml文件名的形式。
2.數(shù)據(jù)的存入必須通過SharedPreferences對(duì)象的編輯器對(duì)象Editor來實(shí)現(xiàn),存入(put)之后與寫入數(shù)據(jù)庫(kù)類似一定要commit。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android實(shí)現(xiàn)3D標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android自定義View實(shí)現(xiàn)仿駕考寶典顯示分?jǐn)?shù)效果(收藏)
本文通過自定義view和屬性動(dòng)畫結(jié)合在一起實(shí)現(xiàn)實(shí)現(xiàn)仿駕考寶典顯示分?jǐn)?shù)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-03-03
RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
android POST數(shù)據(jù)遇到的UTF-8編碼(亂碼)問題解決辦法
這篇文章主要介紹了android POST數(shù)據(jù)遇到的UTF-8編碼(亂碼)問題解決辦法,需要的朋友可以參考下2014-04-04
Android編程實(shí)現(xiàn)WebView自適應(yīng)全屏方法小結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)WebView自適應(yīng)全屏方法,結(jié)合實(shí)例形式總結(jié)了三種常用的WebView自適應(yīng)全屏實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼
這篇文章主要介紹了Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼,通過使用AsyncTask異步任務(wù)實(shí)現(xiàn),調(diào)用publishProgress()方法刷新進(jìn)度來實(shí)現(xiàn),具體代碼大家參考下本文2018-04-04
Android中替換WebView加載網(wǎng)頁(yè)失敗時(shí)的頁(yè)面
這篇文章主要介紹了Android中替換WebView加載網(wǎng)頁(yè)失敗時(shí)的頁(yè)面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01
android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時(shí)必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06

