Android(安卓)SharedPreferences工具类
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;import java.util.ArrayList;
import java.util.List;
import java.util.Map;/**
* Created by LQD on 2018/3/12.
*/public class SPUtils {
public final static String FILLNAME = "config";/**
* 存入某个key对应的value值 * * @param context * @param key * @param value
*/
public static void put(String key, Object value) {
SharedPreferences sp = MainApp.getInstance().getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
if (value instanceof String) {
edit.putString(key, (String) value);
} else if (value instanceof Integer) {
edit.putInt(key, (Integer) value);
} else if (value instanceof Boolean) {
edit.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
edit.putFloat(key, (Float) value);
} else if (value instanceof Long) {
edit.putLong(key, (Long) value);
}
SharedPreferencesCompat.EditorCompat.getInstance().apply(edit);
}
public staticvoid setDataList(String tag, List datalist) {
SharedPreferences sp = MainApp.getInstance().getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (null == datalist || datalist.size() <= 0)
return;
Gson gson = new Gson();
//转换成json数据,再保存
String strJson = gson.toJson(datalist);
editor.putString(tag, strJson);
SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
}
/**
* 获取List
* @param tag
* @return
*/
public staticList getDataList(String tag) {
Listdatalist = new ArrayList ();
SharedPreferences sp = MainApp.getInstance().getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
String strJson = sp.getString(tag, null);
if (null == strJson) {
return datalist;
}
Gson gson = new Gson();
datalist = gson.fromJson(strJson, new TypeToken>() {
}.getType());
return datalist;}
/**
* 得到某个key对应的值 * * @param context * @param key * @param defValue * @return
*/
public static Object get(String key, Object defValue) {
SharedPreferences sp = MainApp.getInstance().getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
if (defValue instanceof String) {
return sp.getString(key, (String) defValue);
} else if (defValue instanceof Integer) {
return sp.getInt(key, (Integer) defValue);
} else if (defValue instanceof Boolean) {
return sp.getBoolean(key, (Boolean) defValue);
} else if (defValue instanceof Float) {
return sp.getFloat(key, (Float) defValue);
} else if (defValue instanceof Long) {
return sp.getLong(key, (Long) defValue);
}
return null;
}/**
* 返回所有数据 * * @param context * @return
*/
public static MapgetAll(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
return sp.getAll();
}/**
* 移除某个key值已经对应的值 * * @param context * @param key
*/
public static void remove(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.remove(key);
SharedPreferencesCompat.EditorCompat.getInstance().apply(edit);
}/**
* 清除所有内容 * * @param context
*/
public static void clear(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.clear();
SharedPreferencesCompat.EditorCompat.getInstance().apply(edit);
}}
更多相关文章
- 一句话锁定MySQL数据占用元凶
- Bmob中的几种查询方法
- 播放记录的SQLite数据库实现
- Android(安卓)ExpandableListActivity
- Android(安卓)SD 卡上创建 SQLite 数据库
- Android_播放器的进度条
- 使用HttpURLConnection请求数据、上传文件、图片等
- GreenDao 连接与关闭Sqlite数据库
- Android中的数据库操作(保证线程安全)