概念
SharePrefences:是Androide自己提供的一個(gè)輕量級(jí)的存儲(chǔ)工具叠纷,適用于存放一些少量的數(shù)據(jù)支示。
介紹使用
- MvpSpUtils 用于讀寫sp文件
- sp中g(shù)etSharedperfences傳入的參數(shù)的區(qū)別 ?
MODE_PRIVATE:默認(rèn)模式,這個(gè)個(gè)模式下主之,該應(yīng)用只能訪問自己創(chuàng)建的文件择吊。再次提交類容時(shí),會(huì)覆蓋上一次提交的內(nèi)容槽奕。
MODE_APPEND:也是該應(yīng)用只能訪問自己創(chuàng)建的文件几睛。但是再次提交文件時(shí),會(huì)寫在上一次提交文件的后面粤攒。
MODE_WORLD_READABLE:會(huì)讓其他應(yīng)用讀到該應(yīng)用創(chuàng)建的文件所森。從API 17開始不推薦使用,是跨進(jìn)程操作夯接,不安全焕济。
MODE_WORLD_WRITEABLE:會(huì)讓其他應(yīng)用寫入文本到該應(yīng)用創(chuàng)建的文件中。也同MODE_WORLD_READABLE盔几,不安全晴弃。 - 文件名默認(rèn)為 "mvp_sp_config"
- saveApply saveCommit 保存
commit:將會(huì)立馬寫入到文件中。
apply:不會(huì)馬上提交逊拍,先存入內(nèi)存中上鞠。開了一個(gè)子線程,把值寫入到文件中芯丧。
在存入數(shù)據(jù)后立馬讀取的話芍阎,apply提交會(huì)有可能讀不出來,commit提交可以讀出來缨恒。
MvpSpUtils.saveApply(SP_VERSION_CODE,mApplication.getPackageManager().getPackageInfo(mApplication.getPackageName(), 0).versionCode);
- remove 刪除
MvpSpUtils.remove(MvpManager.SP_VERSION_CODE);
- getString getLong getInt 獲取
MvpSpUtils.getString(MvpManager.SP_VERSION_CODE)
SharedPreferences工具類 - MvpSpUtils
package com.example.mvplib.utils;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.mvplib.manager.MvpManager;
public class MvpSpUtils {
private static final String DEFAULT_SP_NAME = "mvp_sp_config";
private static final int TYPE_COMMIT = 0X100;
private static final int TYPE_APPLY = 0X101;
public static String getString(String key){
return getString(DEFAULT_SP_NAME, key);
}
public static String getString(String spName,String key){
return MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE).getString(key,null);
}
public static long getLong(String key){
return getLong(DEFAULT_SP_NAME, key);
}
public static long getLong(String spName,String key){
return MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE).getLong(key,-1);
}
public static int getInt(String key){
return getInt(DEFAULT_SP_NAME, key);
}
public static int getInt(String spName,String key){
return MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE).getInt(key,-1);
}
public static void remove(String key){
remove(DEFAULT_SP_NAME,key);
}
public static void remove(String spName,String key){
SharedPreferences preferences = MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove(key);
editor.commit();
}
public static void saveCommit(String key, Object value){
saveString(DEFAULT_SP_NAME,key,value,TYPE_COMMIT);
}
public static void saveCommit(String spName, String key, Object value){
saveString(spName,key,value,TYPE_COMMIT);
}
public static void saveApply(String spName, String key, Object value){
saveString(spName,key,value,TYPE_APPLY);
}
public static void saveApply(String key, Object value){
saveString(DEFAULT_SP_NAME,key,value,TYPE_APPLY);
}
private static void saveString(String spName,String key,Object value,int type){
SharedPreferences preferences = MvpManager.getContext().getSharedPreferences(spName,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if(value instanceof String){
editor.putString(key,value.toString());
}else if(value instanceof Float){
editor.putFloat(key, (Float) value);
}else if(value instanceof Integer){
editor.putInt(key, (Integer) value);
}else if(value instanceof Boolean){
editor.putBoolean(key, (Boolean) value);
}else if(value instanceof Long){
editor.putLong(key, (Long) value);
}
if(type == TYPE_APPLY){
editor.apply();
}else{
editor.commit();
}
}
}