Android SharedPreferences存儲(chǔ)用法詳解
先看Demo運(yùn)行效果
SharedPreferences詳解
SharedPreferences是Android平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類,用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出.
SharedPreferences提供了java常規(guī)的Long、Int、String等類型數(shù)據(jù)的保存接口.
SharedPreferences類似過(guò)去Windows系統(tǒng)上的ini配置文件,但是它分為多種權(quán)限,可以全局共享訪問(wèn)。
提示最終是以xml方式來(lái)保存,整體效率來(lái)看不是特別的高,對(duì)于常規(guī)的輕量級(jí)而言比SQLite要好不少,如果真的存儲(chǔ)量不大可以考慮自己定義文件格式。xml處理時(shí)Dalvik會(huì)通過(guò)自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對(duì)于內(nèi)存資源占用比較好.
SharedPreferences數(shù)據(jù)的四種操作模式
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問(wèn),在該模式下,寫(xiě)入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容
Context.MODE_APPEND:模式會(huì)檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來(lái)控制其他應(yīng)用是否有權(quán)限讀寫(xiě)該文件.
MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取.
MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫(xiě)入
SharedPreferences使用步驟
SharedPreferences的使用非常簡(jiǎn)單,使用SharedPreferences保存key-value對(duì)的步驟如下:
?。?)使用Activity類的getSharedPreferences方法獲得SharedPreferences對(duì)象,其中存儲(chǔ)key-value的文件的名稱由getSharedPreferences方法的第一個(gè)參數(shù)指定.
?。?)使用SharedPreferences接口的edit獲得SharedPreferences.Editor對(duì)象.
?。?)通過(guò)SharedPreferences.Editor接口的putXxx方法保存key-value對(duì)。其中Xxx表示不同的數(shù)據(jù)類型。例如:字符串類型的value需要用putString方法.
?。?)通過(guò)SharedPreferences.Editor接口的commit方法保存key-value對(duì)。commit方法相當(dāng)于數(shù)據(jù)庫(kù)事務(wù)中的提交(commit)操作.
具體代碼的書(shū)寫(xiě)流程為:
A、存放數(shù)據(jù)信息
1、打開(kāi)Preferences,名稱為config,如果存在則打開(kāi)它,否則創(chuàng)建新的Preferences
SharedPreferencesconfig= getSharedPreferences(“config”, 0);
2、讓config處于編輯狀態(tài)
SharedPreferences.Editor editor =config.edit();
3、存放數(shù)據(jù)
editor.putString(“name”,”ATAAW”);
editor.putString(“URL”,”ATAAW.COM”);
4、完成提交
editor.commit();
B、讀取數(shù)據(jù)信息
1、獲取Preferences
SharedPreferencesconfig= getSharedPreferences(“config”, 0);
2、取出數(shù)據(jù)
String name =config.getString(“name”,”默認(rèn)值”);
String url = config.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法
demo的實(shí)現(xiàn)
MainActivity布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.duanlian.sharedpreferencesdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="用戶名" android:textSize="23sp" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="密 碼" android:textSize="23sp" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <Button android:onClick="save" android:layout_width="match_parent" android:layout_height="50dp" android:text="保存信息"/> <Button android:onClick="change" android:layout_width="match_parent" android:layout_height="50dp" android:text="跳轉(zhuǎn)"/> </LinearLayout>
ActivityA的布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.duanlian.sharedpreferencesdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="用戶名" android:textSize="23sp" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="密 碼" android:textSize="23sp" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <Button android:onClick="get" android:layout_width="match_parent" android:layout_height="50dp" android:text="得到信息"/> </LinearLayout>
MainActivity里存儲(chǔ)的具體實(shí)現(xiàn)
都是按照上面寫(xiě)的步驟來(lái)實(shí)現(xiàn)的,最后一定要提交
package com.duanlian.sharedpreferencesdemo; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText passWors; private EditText userName; private SharedPreferences preferences; private SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { userName = (EditText) findViewById(R.id.username); passWors = (EditText) findViewById(R.id.password); //1,實(shí)例化SharedPreferences對(duì)象,參數(shù)1:保存的名字,參數(shù)2:保存的模式MODE_PRIVATE私有的 preferences = getSharedPreferences("config", MODE_PRIVATE); //2,讓SharedPreferences處于可編輯狀態(tài) editor = preferences.edit(); } /** * 保存按鈕的監(jiān)聽(tīng) * @param view */ public void save(View view) { //拿到用戶輸入的數(shù)據(jù) String name = userName.getText().toString().trim(); String pwd = passWors.getText().toString().trim(); //3,儲(chǔ)存數(shù)據(jù),類似于map editor.putString("name", name); editor.putString("pwd", pwd); //4,提交 editor.commit(); } /** * 跳轉(zhuǎn)的點(diǎn)擊監(jiān)聽(tīng) * @param view */ public void change(View view) { Intent intent = new Intent(MainActivity.this, ActivityA.class); startActivity(intent); } }
取出數(shù)據(jù)的實(shí)現(xiàn)
package com.duanlian.sharedpreferencesdemo; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class ActivityA extends AppCompatActivity { private EditText userName; private EditText passWord; private String name; private String pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); //得到數(shù)據(jù) //1,得到SharedPreferences對(duì)象 SharedPreferences preferences = getSharedPreferences("config", 0); //2,取出數(shù)據(jù) name = preferences.getString("name",""); pwd = preferences.getString("pwd", ""); userName = (EditText) findViewById(R.id.username); passWord = (EditText) findViewById(R.id.password); } /** * 得到數(shù)據(jù)按鈕的監(jiān)聽(tīng) * @param view */ public void get(View view) { userName.setText(name); passWord.setText(pwd); } }
demo下載地址:SharedPreferences
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
- Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實(shí)例代碼)
- android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)的操作方法
- Android應(yīng)用開(kāi)發(fā)SharedPreferences存儲(chǔ)數(shù)據(jù)的使用方法
- Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫(kù)中(Saving Data in SQL Databases)
- Android 實(shí)現(xiàn)永久保存數(shù)據(jù)的方法詳解
- Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼
相關(guān)文章
Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)自定義新聞加載頁(yè)面功能,結(jié)合具體實(shí)例形式分析了Android界面加載及頁(yè)面布局相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與裁剪圖片功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫(kù)與裁剪圖片功能,結(jié)合實(shí)例形式分析了Android針對(duì)圖形的旋轉(zhuǎn)與剪切等具體操作技巧,需要的朋友可以參考下2017-01-01Android EditText 實(shí)現(xiàn)監(jiān)聽(tīng)實(shí)例
本文主要介紹Android EditText 組件 實(shí)現(xiàn)監(jiān)聽(tīng)事件,并附有代碼實(shí)例,在Android開(kāi)發(fā)過(guò)程中如果能用到可以參考下2016-07-07Android開(kāi)發(fā)Jetpack組件ViewModel與LiveData使用講解
Jetpack是一個(gè)由多個(gè)技術(shù)庫(kù)組成的套件,可幫助開(kāi)發(fā)者遵循最佳做法,減少樣板代碼并編寫(xiě)可在各種Android版本和設(shè)備中一致運(yùn)行的代碼,讓開(kāi)發(fā)者精力集中編寫(xiě)重要的代碼2022-09-09android獲得當(dāng)前view在屏幕中坐標(biāo)的方法
這篇文章主要介紹了android獲得當(dāng)前view在屏幕中坐標(biāo)的方法,涉及Android針對(duì)view坐標(biāo)相關(guān)屬性的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android動(dòng)態(tài)更換應(yīng)用圖標(biāo)詳情
這篇文章主要介紹了Android動(dòng)態(tài)更換應(yīng)用圖標(biāo)詳情,文章圍繞主題展開(kāi)詳細(xì)的介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07SwipeRefreshLayout+RecyclerView實(shí)現(xiàn)上拉刷新和下拉刷新功能
這篇文章主要介紹了SwipeRefreshLayout+RecyclerView實(shí)現(xiàn)上拉刷新和下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01