package com.woqi.caigou.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Base64;
import com.google.gson.Gson;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
/**
* --------------------------------------------
* 描 述 :
* 1.SharedPreferences工具类
* 封装了对Sp数据存储的基本操作
* -------------------------------------------
*/
public class SpUtils {
private final static String SP_NAME = "share_data";
private static SharedPreferences sp;
private static SharedPreferences getSp(Context context) {
if (sp == null) {
sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
}
return sp;
}
/**
* 获取boolean 数据
*
* @param context
* @param key
* @return 如果没有值,返回false
*/
public static boolean getBoolean(Context context, String key) {
SharedPreferences sp = getSp(context);
return sp.getBoolean(key, false);
}
/**
* 获取boolean 数据
*
* @param context
* @param key
* @param defValue
* @return
*/
public static boolean getBoolean(Context context, String key, boolean defValue) {
SharedPreferences sp = getSp(context);
return sp.getBoolean(key, defValue);
}
/**
【Android|数据保存类型管理】* 存boolean缓存
*
* @param context
* @param key
* @param value
*/
public static void setBoolean(Context context, String key, boolean value) {
SharedPreferences sp = getSp(context);
Editor editor = sp.edit();
editor.putBoolean(key, value);
editor.commit();
}
/**
* 获取String 数据
*
* @param context
* @param key
* @return 如果没有值,返回null
*/
public static String getString(Context context, String key) {
SharedPreferences sp = getSp(context);
return sp.getString(key, null);
}
/**
* 获取String 数据
*
* @param context
* @param key
* @param defValue
* @return
*/
public static String getString(Context context, String key, String defValue) {
SharedPreferences sp = getSp(context);
return sp.getString(key, defValue);
}
/**
* 存String缓存
*
* @param context
* @param key
* @param value
*/
public static void setString(Context context, String key, String value) {
SharedPreferences sp = getSp(context);
Editor editor = sp.edit();
editor.putString(key, value);
editor.commit();
}
/**
* 获取int 数据
*
* @param context
* @param key
* @return 如果没有值,返回-1
*/
public static int getInt(Context context, String key) {
SharedPreferences sp = getSp(context);
return sp.getInt(key, -1);
}
/**
* 获取int 数据
*
* @param context
* @param key
* @param defValue
* @return
*/
public static int getInt(Context context, String key, int defValue) {
SharedPreferences sp = getSp(context);
return sp.getInt(key, defValue);
}
/**
* 存int缓存
*
* @param context
* @param key
* @param value
*/
public static void setInt(Context context, String key, int value) {
SharedPreferences sp = getSp(context);
Editor editor = sp.edit();
editor.putInt(key, value);
editor.commit();
}
/**
* 获取int 数据
*
* @param context
* @param key
* @return 如果没有值,返回-1
*/
public static long getLong(Context context, String key) {
SharedPreferences sp = getSp(context);
return sp.getLong(key, -1);
}
/**
* 获取int 数据
*
* @param context
* @param key
* @param defValue
* @return
*/
public static long getLong(Context context, String key, long defValue) {
SharedPreferences sp = getSp(context);
return sp.getLong(key, defValue);
}
/**
* 存int缓存
*
* @param context
* @param key
* @param value
*/
public static void setLong(Context context, String key, long value) {
SharedPreferences sp = getSp(context);
Editor editor = sp.edit();
editor.putLong(key, value);
editor.commit();
}
public static float getFloat(Context context, String key) {
SharedPreferences sp = getSp(context);
return sp.getFloat(key, 0);
}
/**
* 获取int 数据
*
* @param context
* @param key
* @param defValue
* @return
*/
public static float getFloat(Context context, String key, float defValue) {
SharedPreferences sp = getSp(context);
return sp.getFloat(key, defValue);
}
/**
* 存int缓存
*
* @param context
* @param key
* @param value
*/
public static void setFloat(Context context, String key, float value) {
SharedPreferences sp = getSp(context);
Editor editor = sp.edit();
editor.putFloat(key, value);
editor.commit();
}
/**
* 保存对象
*
* @param context
* @param object保存的对象
*/
public static void setObject(Context context, Object object, String key) throws Exception{
String strJson = "";
Gson gson = new Gson();
strJson = gson.toJson(object);
SpUtils.setString(context, key,strJson);
}
/**
* 获取对象
*
* @param context
* @param key
* @return
*/
public static Object getObject(Context context, String key, Class clazz) throws Exception{
Object obj = null;
String str = getSp(context).getString(key, "");
if (!TextUtils.isEmpty(str)) {
Gson gson = new Gson();
obj = (Object) gson.fromJson(str, clazz);
}
return obj;
}
/**
* 保存List对象集合
* @param context
* @param key
* @param datas
*/
public static void setListObj(Context context, String key, List> datas) {
JSONArray mJsonArray = new JSONArray();
for (int i = 0;
i < datas.size();
i++) {
Object bean = datas.get(i);
mJsonArray.put(bean);
}
Editor editor = getSp(context).edit();
editor.putString(key, mJsonArray.toString());
editor.commit();
}
/**
* 获取本地List持久化数据
*
* @paramcontext
* @paramkey
* @return
*/
public static List> getListObj(Context context , String key ){
List