SharePerference 使用詳解

和你一起終身學(xué)習(xí),這里是程序員Android
經(jīng)典好文推薦,通過(guò)閱讀本文,您將收獲以下知識(shí)點(diǎn):
一、 SharedPreferences 簡(jiǎn)介
二、SharedPreferences 保存數(shù)據(jù)的方法
三、SharedPreferences讀取數(shù)據(jù)的方法
四、總結(jié)SharePerference Utils 封裝類(lèi)使用方法
五、SharedPreferences 數(shù)據(jù)保存位置
一、 SharedPreferences 簡(jiǎn)介
SharedPreferences是Android的一個(gè)接口類(lèi),是Android 數(shù)據(jù)存儲(chǔ)(保存內(nèi)部)的一種方法。主要以*.xml的形式保存在Android /data/data/com.***包名/shared_prefs下,SharedPreferences 類(lèi)提供了一個(gè)通用框架,以便用戶能夠保存和檢索原始數(shù)據(jù)類(lèi)型的鍵值對(duì),原始數(shù)據(jù)類(lèi)型如下:Boolean,Int,Float,Long,String。
1.SharedPreferences 使用方法如下:
創(chuàng)建保存數(shù)據(jù)的
xml文件使用
Editor向xml文件中保存數(shù)據(jù)commit()保存數(shù)據(jù)xml保存地方/data/data/com.***包名/shared_prefs
二、SharedPreferences 保存數(shù)據(jù)的方法
主要使用putBoolean() 和 putString()、putInt()等方法添加值。
三、SharedPreferences讀取數(shù)據(jù)的方法
主要使用 getBoolean()和 getString()、getInt()等 獲取保存的數(shù)據(jù)
四、總結(jié)SharePerference Utils 封裝類(lèi)使用方法
1.移除SharePerference 保存的值
private static SharedPreferences sp;
private static String SPXMLNAME = "sp_config";
/**
* @param ctx
* 上下文環(huán)境
* @param key
* 要從config.xml移除節(jié)點(diǎn)的name的名稱(chēng)
*/
public static void removeKey(Context ctx, String key) {
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().remove(key).commit();
}
2.保存,獲取 Boolean 類(lèi)型值方法
// 1,存儲(chǔ)boolean變量方法
public static void putBoolean(Context ctx, String key, boolean value) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().putBoolean(key, value).commit();
}
// 2,讀取boolean變量方法
public static boolean getBoolean(Context ctx, String key, boolean defValue) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
return sp.getBoolean(key, defValue);
}
3.保存,獲取String類(lèi)型值方法
public static void putString(Context ctx, String key, String value) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().putString(key, value).commit();
}
public static String getString(Context ctx, String key, String defValue) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
return sp.getString(key, defValue);
}
4.保存,獲取Int類(lèi)型值方法
//
public static void putInt(Context ctx, String key, int value) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().putInt(key, value).commit();
}
public static int getInt(Context ctx, String key, int defValue) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
return sp.getInt(key, defValue);
}
5. SharePerferenceUtils
package com.programandroid.Utils;
import android.content.Context;
import android.content.SharedPreferences;
/*
* SharePerferenceUtils.java
*
* Created on: 2017-9-24
* Author: wangjie
*
* Welcome attention to weixin public number get more info
*
* WeiXin Public Number : ProgramAndroid
* 微信公眾號(hào) :程序員Android
*
*/
public class SharePerferenceUtils {
private static SharedPreferences sp;
private static String SPXMLNAME = "sp_config";
/**
* @param ctx
* 上下文環(huán)境
* @param key
* 要從config.xml移除節(jié)點(diǎn)的name的名稱(chēng)
*/
public static void removeKey(Context ctx, String key) {
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().remove(key).commit();
}
// 1,存儲(chǔ)boolean變量方法
public static void putBoolean(Context ctx, String key, boolean value) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().putBoolean(key, value).commit();
}
// 2,讀取boolean變量方法
public static boolean getBoolean(Context ctx, String key, boolean defValue) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
return sp.getBoolean(key, defValue);
}
public static void putString(Context ctx, String key, String value) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().putString(key, value).commit();
}
public static String getString(Context ctx, String key, String defValue) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
return sp.getString(key, defValue);
}
//
public static void putInt(Context ctx, String key, int value) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
sp.edit().putInt(key, value).commit();
}
public static int getInt(Context ctx, String key, int defValue) {
// name存儲(chǔ)文件名稱(chēng)
if (sp == null) {
sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
}
return sp.getInt(key, defValue);
}
}
6. Activity 類(lèi)中使用方法如下:
1.保存數(shù)據(jù)
保存數(shù)據(jù)調(diào)用方法如下:
SharePerferenceUtils.putInt(getApplicationContext(), "int_key", 1);
2.獲取數(shù)據(jù)
獲取數(shù)據(jù)調(diào)用方法如下:
SharePerferenceUtils.getString(getApplicationContext(), "string_key", "default_values");
五、SharedPreferences 數(shù)據(jù)保存位置
SharedPreferences保存在app內(nèi)部(/data/data/com.***包名/shared_prefs),當(dāng)手動(dòng)清除APK 數(shù)據(jù)的時(shí)候,保存的數(shù)據(jù)會(huì)被清除掉
至此 SharedPreferences的使用方法以基本完成。
至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺(jué)得很優(yōu)秀,歡迎點(diǎn)擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請(qǐng)聯(lián)系小編刪除。同時(shí)感謝您的閱讀,期待您的關(guān)注。
