spUtil /** * Created by daydreamty on 2017/4/7. * * @description: SharedPreference工具类 */ public class SpUtil{ private static final String SP_KEY = "";//你的key名 public static void putString(Context ctx, String key, String value) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); sp.edit().putString(key, value).apply(); } public static String getString(Context ctx, String key, String defaultvalue) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); return sp.getString(key, defaultvalue); } public static void putInt(Context ctx, String key, int value) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); sp.edit().putInt(key, value).apply(); } public static int getInt(Context ctx, String key, int defaultvalue) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); return sp.getInt(key, defaultvalue); } public static void putBoolean(Context ctx, String key, Boolean value) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); sp.edit().putBoolean(key, value).apply(); } public static Boolean getBoolean(Context ctx, String key, Boolean defaultvalue) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); return sp.getBoolean(key, defaultvalue); } public static void remove(Context ctx, String key) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); sp.edit().remove(key).apply(); } public static void removeAll(Context ctx) { SharedPreferences sp = ctx.getSharedPreferences(SP_KEY, Context.MODE_PRIVATE); sp.edit().clear().apply(); } }
GsonUtil
/** * Created by daydr on 2017/5/5. * * @description: Gson工具 */ public class GsonUtil { private static Gson gson = null; static { if (gson == null) { gson = new Gson(); } } private GsonUtil() { } /** * 转成json * * @param object 待转换的对象 * @return 返回值 */ public static String GsonString(Object object) { String gsonString = null; if (gson != null) { gsonString = gson.toJson(object); } return gsonString; } /** * 转成bean * * @param gsonString 待转换的json * @param cls Bean.class * @return 返回值 */ public static <T> T GsonToBean(String gsonString, Class<T> cls) { T t = null; if (gson != null) { t = gson.fromJson(gsonString, cls); } return t; } /** * 转成list * * @param gsonString 待转换的json * @param cls 转换的目标 * @return 返回值 */ public static <T> List<T> GsonToList(String gsonString, Class<T> cls) { List<T> list = null; if (gson != null) { list = gson.fromJson(gsonString, new TypeToken<List<T>>() { }.getType()); } return list; } /** * 转成list中有map的 * * @param gsonString 待转换的json * @return 返回值 */ public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) { List<Map<String, T>> list = null; if (gson != null) { list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() { }.getType()); } return list; } /** * 转成map的 * * @param gsonString 待转换的json * @return 返回值 */ public static <T> Map<String, T> GsonToMaps(String gsonString) { Map<String, T> map = null; if (gson != null) { map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() { }.getType()); } return map; } }
VersionUtil
/** * Created by daydreamty on 2017/11/13. * * @description 版本检测util */ public class VersionUtil { // 以下是获得版本信息的工具方法 //版本名 public static String getVersionName(Context context) { return getPackageInfo(context).versionName; } //版本号 public int getVersionCode(Context context) { return getPackageInfo(context).versionCode; } private static PackageInfo getPackageInfo(Context context) { PackageInfo pi = null; try { PackageManager pm = context.getPackageManager(); pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS); return pi; } catch (Exception e) { e.printStackTrace(); } return pi; }}