使用配置:
ShareUtil 工具类。
使用方法:
1. 添加缓存数据 ShareUtil.put(object,object); 用什么参数在工具类直接设置
2. 获取缓存数据 ShareUtil.get(" "); 括号里面添加参数。
public class ShareUtil { private static SharedPreferences sharedPreferences = null; /** * 构造函数 */ public ShareUtil() { sharedPreferences = SgApplication.get().getSharedPreferences("SP_" + "神工", Context.MODE_PRIVATE); } public static SharedPreferences getInstance() { if (sharedPreferences == null) { new ShareUtil(); } return sharedPreferences; } /** * 保存数据 * * @param context * @param key * @param value */ public static void putData(String key, int value) { getInstance().edit().putInt(key, value).commit(); } public static void putData(String key, long value) { getInstance().edit().putLong(key, value).commit(); } public static void putData(String key, String value) { getInstance().edit().putString(key, value).commit(); } public static void putData(String key, boolean value) { getInstance().edit().putBoolean(key, value).commit(); } /** * 获取数据 * * @param context * @param key */ public static String getString(String key) { return getInstance().getString(key, ""); } public static int getInt(String key) { return getInstance().getInt(key, -1); } public static long getLong(String key) { return getInstance().getLong(key, 0L); } /** * 没有值默认是false * * @param context * @param key * @return */ public static boolean getBoolean(String key) { return getInstance().getBoolean(key, false); } /** * 没有值默认是 true * * @param context * @param key * @return */ public static boolean getBooleanTrue(String key) { return getInstance().getBoolean(key, true); } public static void clear() { getInstance().edit().clear().commit(); } }