1. 程式人生 > >SP的封裝(數據持久化方式一)

SP的封裝(數據持久化方式一)

存儲 etl xml文件 ava lang mini mode content xtend

1.先看一段描述:

Interface for accessing and modifying preference data returned by Context.getSharedPreferences(java.lang.String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor

object to ensure the preference values remain in a consistent state and control when they are committed to storage

這段描述講的是sp(SharedPreferences實例)獲取方式:Context.getSharedPreferences(java.lang.String, int),這個方法有兩個參數,第一個表示sp對應xml的文件名,第二個為這個文件的模式,私有,共有,可讀,可寫

修改sp需要使用編輯器:SharedPreferences.Editor,然修改後要提交:edit.commit

2.這個sp實例為整個應用程序共享,從sp維護的xml文件中,可以存取各種類型數據

3.先看看我封裝SP工具類的代碼,這個封裝經過測試,可以很方便使用sp存取數據

package com.market.sp;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Set;

import static android.R.id.edit;

/**
 * 對SharedPreference的封裝
 * 在包名目錄下創建一個shared_pres目錄,並維護一個config.xml文件
 * 所有數據的讀取和存入都是對這個文件的操作
 * Created by Administrator on 2017/6/15.
 
*/ public class SPUtils { private static SharedPreferences sp = null; /** * 將一個boolean值存入sp文件中 * @param ctx 上下文 * @param key 存儲節點名稱 * @param value 存儲節點的值 */ public static void putBoolean(Context ctx, String key, boolean value){ //如果sp為空,則獲取創建一個sp對象 if(sp == null){ sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE); } sp.edit().putBoolean(key,value).commit();//獲取sp編輯器,放入bool值,並提交 } /** * 根據key讀取一個boolean值value,沒有的話使用defvalue代替 * @param ctx * @param key * @param defvalue */ public static boolean getBoolean(Context ctx, String key, boolean defvalue){ //如果sp為空,則獲取創建一個sp對象 if(sp == null){ sp = ctx.getSharedPreferences("config",Context.MODE_PRIVATE); } boolean b = sp.getBoolean(key, defvalue); return b; } /** * 將一個String值存入sp文件中 * @param context 上下文 * @param key 存儲節點名稱 * @param value 存儲節點的值 */ public static void putString(Context context,String key,String value){ if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } sp.edit().putString(key, value).commit(); } /** * 從sp中根據key取出String值 * @param context 上下文 * @param key 存儲節點名稱 * @param defValue 存儲節點默認值 * @return string */ public static String getString(Context context,String key,String defValue){ if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } String string = sp.getString(key, defValue); return string; } /** * 移除sp中的一個節點 * @param context 上下文環境 * @param key 節點名稱 */ public static void removeFromSP(Context context, String key) { if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } final SharedPreferences.Editor edit = sp.edit(); edit.remove(key); } /** * 從sp中根據key取出int值 * @param context * @param key * @param defValue * @return */ public static int getInt(Context context, String key, int defValue) { if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } int i = sp.getInt(key, defValue); return i; } /** * 將一個int值存入sp文件中 * @param context * @param key * @param value */ public static void putInt(Context context,String key,int value){ if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } sp.edit().putInt(key, value).commit(); } /** * 從sp中根據key取出float值 * @param context * @param key * @param defValue * @return */ public static float getFloat(Context context, String key, float defValue) { if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } float i = sp.getFloat(key, defValue); return i; } /** * 將一個float值存入sp文件中 * @param context * @param key * @param value */ public static void putFloat(Context context,String key,float value){ if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } sp.edit().putFloat(key,value).commit(); } /** * 從sp中根據key取出int值 * @param context * @param key * @param defValue * @return */ public static Set<String> getStringSet(Context context, String key, Set<String> defValue) { if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } Set<String> sets = sp.getStringSet(key, defValue); return sets; } /** * 將一個int值存入sp文件中 * @param context * @param key * @param sets */ public static void putStringSet(Context context,String key,Set<String> sets){ if(sp == null){//如果sp文件不存在,則創建該文件 sp = context.getSharedPreferences("config", Context.MODE_PRIVATE); } sp.edit().putStringSet(key,sets).commit(); } }

4.測試代碼:

package com.market.sp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SPUtils.putBoolean(this,"ismy",true);
        SPUtils.putFloat(this,"myfloat",23.45f);

        Log.e(getLocalClassName(),SPUtils.getBoolean(this,"ismy",false)+"");
        Log.e(getLocalClassName(),SPUtils.getFloat(this,"myfloat",0.0f)+"");
    }

5.運行效果展示

打印結果

技術分享

文件結果:可以看到在/data/data/com.market.sp/生成了shared_prefs目錄,且在該目錄下生成config.xml文件

技術分享

文件內容查看:可見存儲了兩個節點float和boolean,而且我們要存儲的數據都在裏面

技術分享

SP的封裝(數據持久化方式一)