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

通過Activity自帶的getSharedPreferences方法,可以得到SharedPreferences對象。
public abstract SharedPreferences getSharedPreferences (String name, int mode);
name:表示保存后 xml 文件的名稱
mode:表示 xml 文檔的操作權(quán)限模式(私有,可讀,可寫),使用0或者MODE_PRIVATE作為默認的操作權(quán)限模式。
1.數(shù)據(jù)讀?。?br />
通過SharedPreferences對象的鍵key可以獲取到對應(yīng)key的鍵值。對于不同類型的鍵值有不同的函數(shù):
getBoolean,getInt,getFloat,getLong. public abstract String getString (String key, String defValue);
2.數(shù)據(jù)存入:
數(shù)據(jù)的存入是通過SharedPreferences對象的編輯器對象Editor來實現(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 ();
下面一個實例顯示一個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();
}
}
幾點說明:
1.SharedPreferences的獲取有兩種方法:
一是上面提到的通過Activity自帶(本質(zhì)來講是Context的)的getSharedPreferences方法,可以得到SharedPreferences對象。這種方法的好處是可以指定保存的xml文件名。
另一種是通過PreferenceManager.getSharedPreferences(Context)獲取SharedPreferences對象。這種方法不能指定保存的xml文件名,文件名使用默認的:<package name>+"_preferences.xml"的形式,不過如果在一個包里面采用這種方式需要保存多個這樣的xml文件,可能會亂掉。建議采用第一種指定xml文件名的形式。
2.數(shù)據(jù)的存入必須通過SharedPreferences對象的編輯器對象Editor來實現(xiàn),存入(put)之后與寫入數(shù)據(jù)庫類似一定要commit。
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android自定義View實現(xiàn)仿駕考寶典顯示分數(shù)效果(收藏)
本文通過自定義view和屬性動畫結(jié)合在一起實現(xiàn)實現(xiàn)仿駕考寶典顯示分數(shù)效果,非常不錯,具有參考借鑒價值,需要的的朋友參考下2017-03-03
RecyclerView實現(xiàn)仿支付寶應(yīng)用管理
這篇文章主要為大家詳細介紹了RecyclerView實現(xiàn)仿支付寶應(yīng)用管理的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
android POST數(shù)據(jù)遇到的UTF-8編碼(亂碼)問題解決辦法
這篇文章主要介紹了android POST數(shù)據(jù)遇到的UTF-8編碼(亂碼)問題解決辦法,需要的朋友可以參考下2014-04-04
Android編程實現(xiàn)WebView自適應(yīng)全屏方法小結(jié)
這篇文章主要介紹了Android編程實現(xiàn)WebView自適應(yīng)全屏方法,結(jié)合實例形式總結(jié)了三種常用的WebView自適應(yīng)全屏實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12
Android中替換WebView加載網(wǎng)頁失敗時的頁面
這篇文章主要介紹了Android中替換WebView加載網(wǎng)頁失敗時的頁面,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01
android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06

