Android SharedPreferences存儲用法詳解
先看Demo運行效果

SharedPreferences詳解
SharedPreferences是Android平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出.
SharedPreferences提供了java常規(guī)的Long、Int、String等類型數(shù)據(jù)的保存接口.
SharedPreferences類似過去Windows系統(tǒng)上的ini配置文件,但是它分為多種權(quán)限,可以全局共享訪問。
提示最終是以xml方式來保存,整體效率來看不是特別的高,對于常規(guī)的輕量級而言比SQLite要好不少,如果真的存儲量不大可以考慮自己定義文件格式。xml處理時Dalvik會通過自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對于內(nèi)存資源占用比較好.
SharedPreferences數(shù)據(jù)的四種操作模式
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
Context.MODE_PRIVATE:為默認操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會覆蓋原文件的內(nèi)容
Context.MODE_APPEND:模式會檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應(yīng)用是否有權(quán)限讀寫該文件.
MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取.
MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫入
SharedPreferences使用步驟
SharedPreferences的使用非常簡單,使用SharedPreferences保存key-value對的步驟如下:
(1)使用Activity類的getSharedPreferences方法獲得SharedPreferences對象,其中存儲key-value的文件的名稱由getSharedPreferences方法的第一個參數(shù)指定.
(2)使用SharedPreferences接口的edit獲得SharedPreferences.Editor對象.
?。?)通過SharedPreferences.Editor接口的putXxx方法保存key-value對。其中Xxx表示不同的數(shù)據(jù)類型。例如:字符串類型的value需要用putString方法.
?。?)通過SharedPreferences.Editor接口的commit方法保存key-value對。commit方法相當(dāng)于數(shù)據(jù)庫事務(wù)中的提交(commit)操作.
具體代碼的書寫流程為:
A、存放數(shù)據(jù)信息
1、打開Preferences,名稱為config,如果存在則打開它,否則創(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”,”默認值”);
String url = config.getString(“URL”,”default”);
以上就是Android中SharedPreferences的使用方法
demo的實現(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里存儲的具體實現(xiàn)
都是按照上面寫的步驟來實現(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,實例化SharedPreferences對象,參數(shù)1:保存的名字,參數(shù)2:保存的模式MODE_PRIVATE私有的
preferences = getSharedPreferences("config", MODE_PRIVATE);
//2,讓SharedPreferences處于可編輯狀態(tài)
editor = preferences.edit();
}
/**
* 保存按鈕的監(jiān)聽
* @param view
*/
public void save(View view) {
//拿到用戶輸入的數(shù)據(jù)
String name = userName.getText().toString().trim();
String pwd = passWors.getText().toString().trim();
//3,儲存數(shù)據(jù),類似于map
editor.putString("name", name);
editor.putString("pwd", pwd);
//4,提交
editor.commit();
}
/**
* 跳轉(zhuǎn)的點擊監(jiān)聽
* @param view
*/
public void change(View view) {
Intent intent = new Intent(MainActivity.this, ActivityA.class);
startActivity(intent);
}
}
取出數(shù)據(jù)的實現(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對象
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)聽
* @param view
*/
public void get(View view) {
userName.setText(name);
passWord.setText(pwd);
}
}
demo下載地址:SharedPreferences
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SharedPreferences實現(xiàn)記住密碼和自動登錄界面
- Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實例代碼)
- android中使用SharedPreferences進行數(shù)據(jù)存儲的操作方法
- Android應(yīng)用開發(fā)SharedPreferences存儲數(shù)據(jù)的使用方法
- Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中(Saving Data in SQL Databases)
- Android 實現(xiàn)永久保存數(shù)據(jù)的方法詳解
- Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實現(xiàn)代碼
相關(guān)文章
玩轉(zhuǎn)AppBarLayout實現(xiàn)更酷炫的頂部欄
玩轉(zhuǎn)AppBarLayout,實現(xiàn)更酷炫的頂部欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android應(yīng)用開發(fā)中CardView的初步使用指南
這篇文章主要介紹了Android應(yīng)用開發(fā)中CardView的初步使用指南,CardView主要處理一些卡片型的視圖布局,需要的朋友可以參考下2016-02-02
Android中SQLite數(shù)據(jù)庫知識點總結(jié)
在本篇文章里小編給大家分享了關(guān)于Android中SQLite數(shù)據(jù)庫知識點總結(jié),有需要的朋友們跟著學(xué)習(xí)下。2019-02-02
Android SQLite數(shù)據(jù)庫中的表詳解
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫中的表詳解的相關(guān)資料,這里附有實例代碼,需要的朋友可以參考下2017-01-01
Android Studio用genymotion運行后小圖標(biāo)無法顯示問題
這篇文章主要介紹了Android Studio用genymotion運行后小圖標(biāo)無法顯示的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android開發(fā)快速實現(xiàn)底部導(dǎo)航欄示例
這篇文章主要為大家介紹了Android開發(fā)快速實現(xiàn)底部導(dǎo)航欄的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
Android實現(xiàn)在一個activity中添加多個listview的方法
這篇文章主要介紹了Android實現(xiàn)在一個activity中添加多個listview的方法,分析了Activity中添加listview的原理與具體實現(xiàn)方法,需要的朋友可以參考下2016-08-08

