记录一下项目用到的工具类
/** * SettingsProvider对数据进行了分类,分别是Global、System、Secure三种类型,它们的区别如下: * Global:所有的偏好设置对系统的所有用户公开,第三方APP有读没有写的权限; * System:包含各种各样的用户偏好系统设置; * Secure:安全性的用户偏好系统设置,第三方APP有读没有写的权限。 */ public class DataUtil { private static volatile DataUtil dataUtil; private Context mContext; private DataUtil(Context mContext){ this.mContext = mContext; } public static DataUtil getInstance(Context context){ if(dataUtil == null){ synchronized (DataUtil.class){ if(dataUtil == null){ dataUtil = new DataUtil(context); } } } return dataUtil; } /** * 保存Global数据 * @param key * @param value */ public void putGlobalData(String key, Object value){ if(value instanceof String){ Settings.Global.putString(mContext.getContentResolver(), key, (String) value); }else if(value instanceof Integer){ Settings.Global.putInt(mContext.getContentResolver(), key, ((Integer) value).intValue()); }else if(value instanceof Long){ Settings.Global.putLong(mContext.getContentResolver(), key, ((Long) value).longValue()); }else if(value instanceof Boolean){ Settings.Global.putInt(mContext.getContentResolver(), key, ((Boolean) value).booleanValue()?1:0); }else{ throw new IllegalArgumentException("not support value type!"); } } /** * 获取Global数据 * @param key * @param defValue 默认值 */ public Object getGlobalData(String key,Object defValue){ if(defValue instanceof String){ String data = Settings.Global.getString(mContext.getContentResolver(), key); return data == null?defValue:data; }else if(defValue instanceof Integer){ return Settings.Global.getInt(mContext.getContentResolver(), key, ((Integer) defValue).intValue()); }else if(defValue instanceof Long){ return Settings.Global.getLong(mContext.getContentResolver(), key, ((Long) defValue).longValue()); }else if(defValue instanceof Boolean){ int intData = Settings.Global.getInt(mContext.getContentResolver(), key, ((Boolean) defValue).booleanValue()?1:0); return intData == 1?true:false; }else { throw new IllegalArgumentException("not support value type!"); } } /** * 保存System数据 * @param key * @param value */ public void putSystemData(String key, Object value){ if(value instanceof String){ Settings.System.putString(mContext.getContentResolver(), key, (String) value); }else if(value instanceof Integer){ Settings.System.putInt(mContext.getContentResolver(), key, ((Integer) value).intValue()); }else if(value instanceof Long){ Settings.System.putLong(mContext.getContentResolver(), key, ((Long) value).longValue()); }else if(value instanceof Boolean){ Settings.System.putInt(mContext.getContentResolver(), key, ((Boolean) value).booleanValue()?1:0); }else{ throw new IllegalArgumentException("not support value type!"); } } /** * 获取System数据 * @param key * @param defValue 默认值 */ public Object getSystemData(String key,Object defValue){ if(defValue instanceof String){ String data = Settings.System.getString(mContext.getContentResolver(), key); return data == null?defValue:data; }else if(defValue instanceof Integer){ return Settings.System.getInt(mContext.getContentResolver(), key, ((Integer) defValue).intValue()); }else if(defValue instanceof Long){ return Settings.System.getLong(mContext.getContentResolver(), key, ((Long) defValue).longValue()); }else if(defValue instanceof Boolean){ int intData = Settings.System.getInt(mContext.getContentResolver(), key, ((Boolean) defValue).booleanValue()?1:0); return intData == 1?true:false; }else { throw new IllegalArgumentException("not support value type!"); } } /** * 保存Secure数据 * @param key * @param value */ public void putSecureData(String key, Object value){ if(value instanceof String){ Settings.Secure.putString(mContext.getContentResolver(), key, (String) value); }else if(value instanceof Integer){ Settings.Secure.putInt(mContext.getContentResolver(), key, ((Integer) value).intValue()); }else if(value instanceof Long){ Settings.Secure.putLong(mContext.getContentResolver(), key, ((Long) value).longValue()); }else if(value instanceof Boolean){ Settings.Secure.putInt(mContext.getContentResolver(), key, ((Boolean) value).booleanValue()?1:0); }else{ throw new IllegalArgumentException("not support value type!"); } } /** * 获取Secure数据 * @param key * @param defValue 默认值 */ public Object getSecureData(String key,Object defValue){ if(defValue instanceof String){ String data = Settings.Secure.getString(mContext.getContentResolver(), key); return data == null?defValue:data; }else if(defValue instanceof Integer){ return Settings.Secure.getInt(mContext.getContentResolver(), key, ((Integer) defValue).intValue()); }else if(defValue instanceof Long){ return Settings.Secure.getLong(mContext.getContentResolver(), key, ((Long) defValue).longValue()); }else if(defValue instanceof Boolean){ int intData = Settings.Secure.getInt(mContext.getContentResolver(), key, ((Boolean) defValue).booleanValue()?1:0); return intData == 1?true:false; }else { throw new IllegalArgumentException("not support value type!"); } } }
标签:instanceof,mContext,Settings,defValue,value,key,工具,SettingsProvider From: https://www.cnblogs.com/mhzf/p/17036399.html