SharedPreference工具类 发表于 2017-11-15 | 分类于 Android代码库 字数统计: 228 字 | 阅读时长 ≈ 1 分钟 SharedPreference工具类 代码1234567891011121314151617181920212223242526272829303132333435363738394041public class SharedPreferenceUtil { public static void putSharedPreferences(String key, Object value) { Context context = AppManager.getInstance().getAppContext(); String type = value.getClass().getSimpleName(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = sharedPreferences.edit(); if ("Integer".equals(type)) { editor.putInt(key, (Integer) value); } else if ("Boolean".equals(type)) { editor.putBoolean(key, (Boolean) value); } else if ("String".equals(type)) { editor.putString(key, (String) value); } else if ("Float".equals(type)) { editor.putFloat(key, (Float) value); } else if ("Long".equals(type)) { editor.putLong(key, (Long) value); } editor.commit(); } public static Object getSharedPreferences(String key, Object defValue) { Context context = AppManager.getInstance().getAppContext(); String type = defValue.getClass().getSimpleName(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); //defValue为为默认值,如果当前获取不到数据就返回它 if ("Integer".equals(type)) { return sharedPreferences.getInt(key, (Integer) defValue); } else if ("Boolean".equals(type)) { return sharedPreferences.getBoolean(key, (Boolean) defValue); } else if ("String".equals(type)) { return sharedPreferences.getString(key, (String) defValue); } else if ("Float".equals(type)) { return sharedPreferences.getFloat(key, (Float) defValue); } else if ("Long".equals(type)) { return sharedPreferences.getLong(key, (Long) defValue); } return null; }}