Android SharedPreferencesUtils数据保存工具类

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SPUtils {
    private static final String TAG = "SPUtils";
    private static SharedPreferences sp_set = null, sp_user = null, sp_cache = null;
    private static final String KEY_USERDATA = "userdata";//用户数据
    private static final String KEY_SET = "setdata";//软件设置数据
    private static final String KEY_CACHE = "cachedata";//缓存数据

    public static void init(Context context) {
        sp_set = context.getSharedPreferences(KEY_SET, Context.MODE_PRIVATE);
        sp_user = context.getSharedPreferences(KEY_USERDATA, Context.MODE_PRIVATE);
        sp_cache = context.getSharedPreferences(KEY_CACHE, Context.MODE_PRIVATE);

    }

    public static void putSetData(String key, Object value) {
        if (sp_set == null) {
            Log.e(TAG, "should call SPUtils.init() in application", new NullPointerException());
            return;
        }
        if (value instanceof Boolean) {
            sp_set.edit().putBoolean(key, (Boolean) value).apply();
        } else if (value instanceof Integer) {
            sp_set.edit().putInt(key, (Integer) value).apply();
        } else if (value instanceof String) {
            sp_set.edit().putString(key, (String) value).apply();
        } else if (value instanceof Float) {
            sp_set.edit().putFloat(key, (Float) value).apply();
        } else if (value instanceof Long) {
            sp_set.edit().putLong(key, (Long) value).apply();
        } else {
            Log.e("SPUtils", "putSetData(保存类型错误)");
        }
    }

    public static void putUserData(String key, Object value) {
        if (sp_user == null) {
            Log.e(TAG, "should call SPUtils.init() in application", new NullPointerException());
            return;
        }
        if (value instanceof Boolean) {
            sp_user.edit().putBoolean(key, (Boolean) value).apply();
        } else if (value instanceof Integer) {
            sp_user.edit().putInt(key, (Integer) value).apply();
        } else if (value instanceof String) {
            sp_user.edit().putString(key, (String) value).apply();
        } else if (value instanceof Float) {
            sp_user.edit().putFloat(key, (Float) value).apply();
        } else if (value instanceof Long) {
            sp_user.edit().putLong(key, (Long) value).apply();
        } else {
            Log.e("SPUtils", "putUserData(保存类型错误)" + value);
        }
    }

    public static void putCacheData(String key, Object value) {
        if (sp_cache == null) {
            Log.e(TAG, "should call SPUtils.init() in application", new NullPointerException());
            return;
        }
        if (value instanceof Boolean) {
            sp_cache.edit().putBoolean(key, (Boolean) value).apply();
        } else if (value instanceof Integer) {
            sp_cache.edit().putInt(key, (Integer) value).apply();
        } else if (value instanceof String) {
            sp_cache.edit().putString(key, (String) value).apply();
        } else if (value instanceof Float) {
            sp_cache.edit().putFloat(key, (Float) value).apply();
        } else if (value instanceof Long) {
            sp_cache.edit().putLong(key, (Long) value).apply();
        } else {
            Log.e(TAG, "putCacheData(保存类型错误)");
        }
    }

    public static SharedPreferences getSetDataSP() {
        return sp_set;
    }

    public static SharedPreferences getUserDataSP() {
        return sp_user;
    }

    public static SharedPreferences getCacheDataSP() {
        return sp_cache;
    }

    public static void ClearSetData() {
        sp_set.edit().clear().apply();
    }

    public static void ClearUserData() {
        sp_user.edit().clear().apply();
    }

    public static void ClearCacheData() {
        sp_cache.edit().clear().apply();
    }

    public static void removeSetData(String key) {
        sp_set.edit().remove(key).apply();
    }

    public static void removeUserData(String key) {
        sp_user.edit().remove(key).apply();
    }

    public static void removeCacheData(String key) {
        sp_cache.edit().remove(key).apply();
    }

    public static <C extends Serializable> C getObject(String key, C defValue) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        C result = defValue;

        String value = sp_user.getString(key, null);
        if (value != null) {
            try {
                byte[] decoded = Base64.decode(value.getBytes(), Base64.DEFAULT);
                bais = new ByteArrayInputStream(decoded);
                ois = new ObjectInputStream(bais);
                result = (C) ois.readObject();

            } catch (Exception e) {
                Log.e(TAG, e.toString());

            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        Log.e(TAG, e.toString());
                    }
                }
                if (bais != null) {
                    try {
                        bais.close();
                    } catch (IOException e) {
                        Log.e(TAG, e.toString());
                    }
                }
            }
        }

        return result;
    }


    public static <C extends Serializable> void putObject(String key, C value) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;

        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(value);
            byte[] encoded = Base64.encode(baos.toByteArray(), Base64.DEFAULT);
            sp_user.edit().putString(key, new String(encoded)).apply();

        } catch (IOException e) {
            Log.e(TAG, e.toString());
            throw new RuntimeException(e);

        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    Log.e(TAG, e.toString());
                }
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/u012390044/article/details/50828118