1. 程式人生 > >SharedPreferences工具類的封裝

SharedPreferences工具類的封裝

package com.moie.newsapp.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SpUtil {

    private static volatile SpUtil inserter;
    private Context context;
    private static String SPCONFIG = "config";
    private static SharedPreferences sp;
    private static Editor edit;

    private SpUtil(Context context) {
        this.context = context;
        sp = context.getSharedPreferences(SPCONFIG, context.MODE_PRIVATE);
        edit = sp.edit();
    }

    public static SpUtil getInserter(Context con) {
        if (inserter == null) {
            synchronized (SpUtil.class) {
                if (null == inserter) {
                    inserter = new SpUtil(con);
                }
            }
        }
        return inserter;
    }

    // 獲取sharedpreferences中的資料
    public  Object getSpData( String key, Object value) {
        String type = value.getClass().getSimpleName();
        if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) value);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) value);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) value);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) value);
        } else if ("String".equals(type)) {
            return sp.getString(key, (String) value);
        }
        return null;
    }

    public  Editor saveData( String key, Object value) {
        String type = value.getClass().getSimpleName();
        if ("Integer".equals(type)) {
            return edit.putInt(key, (Integer) value);
        } else if ("Float".equals(type)) {
            return edit.putFloat(key, (Float) value);
        } else if ("Boolean".equals(type)) {
            return edit.putBoolean(key, (Boolean) value);
        } else if ("Long".equals(type)) {
            return edit.putLong(key, (Long) value);
        } else if ("String".equals(type)) {
            return edit.putString(key, (String) value);
        }
        return null;

    }

    public  boolean commit() {
        return edit.commit();
    }
}

使用方法 

SpUtil.getInserter(getActivity()).saveData("data", data).commit();//儲存資料
String data = (String) SpUtil.getInserter(getActivity()).getSpData("data", "");//獲取資料