Android中SharedPreference詳解及簡單實(shí)例
Android中SharedPreference詳解
SharedPreference是Android提供的一種輕量級的數(shù)據(jù)存儲方式,主要用來存儲一些簡單的配置信息,例如,默認(rèn)歡迎語,登錄用戶名和密碼等。其以鍵值對的方式存儲,使得我們能很方便進(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對象。
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對象的鍵key可以獲取到對應(yīng)key的鍵值。對于不同類型的鍵值有不同的函數(shù):
getBoolean,getInt,getFloat,getLong. public abstract String getString (String key, String defValue);
2.數(shù)據(jù)存入:
數(shù)據(jù)的存入是通過SharedPreferences對象的編輯器對象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 ();
下面一個實(shí)例顯示一個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對象。這種方法的好處是可以指定保存的xml文件名。
另一種是通過PreferenceManager.getSharedPreferences(Context)獲取SharedPreferences對象。這種方法不能指定保存的xml文件名,文件名使用默認(rèn)的:<package name>+"_preferences.xml"的形式,不過如果在一個包里面采用這種方式需要保存多個這樣的xml文件,可能會亂掉。建議采用第一種指定xml文件名的形式。
2.數(shù)據(jù)的存入必須通過SharedPreferences對象的編輯器對象Editor來實(shí)現(xiàn),存入(put)之后與寫入數(shù)據(jù)庫類似一定要commit。
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
AndroidStudio安全管理簽名文件keystroe和簽名密碼(星空武哥)
我們在使用AndroidStudio進(jìn)行release版的apk簽名的時(shí)候,往往都是將簽名文件keystore放在項(xiàng)目中,密碼寫在build.gradle中,keystore和密碼就隨著代碼上傳到了Git倉庫中了,這樣往往很不安全,因?yàn)檫@樣被人獲取2017-09-09Android中Glide加載圖片并實(shí)現(xiàn)圖片緩存
本篇文章主要介紹了Android中Glide加載圖片并實(shí)現(xiàn)圖片緩存,這里和大家簡單的分享一下Glide的使用方法以及緩存 ,有興趣的可以了解一下。2017-03-03android實(shí)現(xiàn)搜索功能并將搜索結(jié)果保存到SQLite中(實(shí)例代碼)
這篇文章主要介紹了android實(shí)現(xiàn)搜索功能并將搜索結(jié)果保存到SQLite中,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android自定義相機(jī)界面的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)界面的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android開發(fā)筆記XML數(shù)據(jù)解析方法及優(yōu)缺點(diǎn)
XML數(shù)據(jù)是一種常見的數(shù)據(jù)格式,Android開發(fā)中需要對其進(jìn)行解析。常用的XML解析方式有DOM、SAX、Pull和Json等,每種方式都有其優(yōu)缺點(diǎn)。開發(fā)者可以根據(jù)具體需求選擇合適的解析方式,提高數(shù)據(jù)解析效率和性能2023-05-05Android自定義View 使用PathMeasure簡單模仿系統(tǒng)ProgressBar(四)
這篇文章主要為大家詳細(xì)介紹了Android自定義View,使用PathMeasure簡單模仿系統(tǒng)ProgressBar,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03java從輸入流中獲取數(shù)據(jù)并返回字節(jié)數(shù)組示例
這篇文章主要介紹了java從輸入流中獲取數(shù)據(jù)并以字節(jié)數(shù)組返回,這是一個常用的方法,以后可以直接拿來用。這種輸入流可以來自Android本地,也可以來自網(wǎng)絡(luò)2014-01-01Android自定義控件實(shí)現(xiàn)優(yōu)雅的廣告輪播圖
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)優(yōu)雅的廣告輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03