import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* created by zyb on 2024/10/29 17:20
*/
public class SPUtils {
/**
* 保存在车机里面的文件名
*/
public static final String FILE_NAME = "share_data_system";
private volatile static SPUtils sp;
public static SharedPreferences.Editor editor;
public static SharedPreferences preferences;
public SPUtils(Context context) {
preferences = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
editor = preferences.edit();
}
public static SPUtils getInstance(Context context) {
if (sp == null) {
synchronized (SPUtils.class) {
if (sp == null) {
sp = new SPUtils(context);
}
}
}
return sp;
}
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
*
* @param key
* @param object
*/
public void put(String key, Object object) {
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
SharedPreferencesCompat.apply(editor);
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
*
* @param key
* @param defaultObject
* @return
*/
public Object get(String key, Object defaultObject) {
if (defaultObject instanceof String) {
return preferences.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return preferences.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return preferences.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return preferences.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return preferences.getLong(key, (Long) defaultObject);
}
return null;
}
/**
* 保存List
*
* @param tag
* @param datalist
*/
public <T> void setDataList(String tag, List<T> datalist) {
if (null == datalist || datalist.size() <= 0)
return;
Gson gson = new Gson();
//转换成json数据,再保存
String strJson = gson.toJson(datalist);
editor.clear();
editor.putString(tag, strJson);
editor.commit();
}
/**
* 获取List
*
* @param tag
* @return
*/
public <T> List<T> getDataList(String tag) {
List<T> datalist = new ArrayList<T>();
String strJson = preferences.getString(tag, null);
if (null == strJson) {
return datalist;
}
Gson gson = new Gson();
datalist = gson.fromJson(strJson, new TypeToken<List<T>>() {
}.getType());
return datalist;
}
/**
* 获取List
*
* @param tag
* @param clazz
* @param <T>
* @return
*/
public <T> List<T> getDataList(String tag, Class<T> clazz) {
List<T> datalist = new ArrayList<T>();
String strJson = preferences.getString(tag, null);
if (null == strJson) {
return datalist;
}
Gson gson = new Gson();
Type type = new ParameterizedTypeImpl(List.class, new Class[]{clazz});
datalist = gson.fromJson(strJson, type);
return datalist;
}
/**
* 移除某个key值已经对应的值
*
* @param context
* @param key
*/
public static void remove(Context context, String key) {
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}
/**
* 清除所有数据
*
* @param context
*/
public static void clear(Context context) {
editor.clear();
SharedPreferencesCompat.apply(editor);
}
/**
* 查询某个key是否已经存在
*
* @param context
* @param key
* @return
*/
public static boolean contains(Context context, String key) {
return preferences.contains(key);
}
/**
* 返回所有的键值对
*
* @param context
* @return
*/
public static Map<String, ?> getAll(Context context) {
return preferences.getAll();
}
/**
* 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
*
* @author zyb
*/
private static class SharedPreferencesCompat {
private static final Method sApplyMethod = findApplyMethod();
/**
* 反射查找apply的方法
*
* @return
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static Method findApplyMethod() {
try {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e) {
}
return null;
}
/**
* 如果找到则使用apply执行,否则使用commit
*
* @param editor
*/
public static void apply(SharedPreferences.Editor editor) {
try {
if (sApplyMethod != null) {
sApplyMethod.invoke(editor);
return;
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
editor.commit();
}
}
class ParameterizedTypeImpl implements ParameterizedType {
private final Class raw;
private final Type[] args;
public ParameterizedTypeImpl(Class raw, Type[] args) {
this.raw = raw;
this.args = args != null ? args : new Type[0];
}
@Override
public Type[] getActualTypeArguments() {
return args;
}
@Override
public Type getRawType() {
return raw;
}
@Override
public Type getOwnerType() {
return null;
}
}
}
标签:return,SPUtils.18604360,param,editor,key,public,String
From: https://www.cnblogs.com/zybao/p/18604362