android中对SharedPreference实现简单的封装
16lz
2021-01-26
public class SharedPreferenceUtil { private static SharedPreferenceUtil sharedPreferenceUtil; private static SharedPreferences sharedPreferences; private final static String KEY = "tsb_camera_config_sharepreferences"; private SharedPreferenceUtil( Context context ) { sharedPreferences = context.getSharedPreferences( KEY , Context.MODE_PRIVATE ); } public static SharedPreferenceUtil getInstance( Context context ) { if ( sharedPreferenceUtil == null ) { sharedPreferenceUtil = new SharedPreferenceUtil( context ); } return sharedPreferenceUtil; } /** * 设置String类型值 * * @param key * @param value */ public void putString( String key , String value ) { Editor editor = sharedPreferences.edit( ); editor.putString( key , value ); editor.commit( ); } /** * 设置long类型值 * * @param key * @param value */ public void putLong( String key , long value ) { Editor editor = sharedPreferences.edit( ); editor.putLong( key , value ); editor.commit( ); } /** * 设置int类型值 * * @param key * @param value */ public void putInt( String key , int value ) { Editor editor = sharedPreferences.edit( ); editor.putInt( key , value ); editor.commit( ); } /** * 设置Boolean类型值 * * @param key * @param value */ public void putBoolean( String key , boolean value ) { Editor editor = sharedPreferences.edit( ); editor.putBoolean( key , value ); editor.commit( ); } /** * 设置Float类型值 * * @param key * @param value */ public void putFloat( String key , float value ) { Editor editor = sharedPreferences.edit( ); editor.putFloat( key , value ); editor.commit( ); } /** * 获取key相对应的value,如果不设默认参数,默认值为"" * * @param key * @return */ public String getString( String key ) { return getString( key , "" ); } /** * 获取key相对应的value,如果不设默认参数,默认值为"" * * @param key * @param defaultValue * @return */ public String getString( String key , String defaultValue ) { return sharedPreferences.getString( key , defaultValue ); } /** * 获取key相对应的value,如果不设默认参数,默认值为false * * @param key * @return */ public boolean getBoolean( String key ) { return getBoolean( key , false ); } /** * 获取key相对应的value,如果不设默认参数,默认值为false * * @param key * @param defalutValue * @return */ public boolean getBoolean( String key , boolean defaultValue ) { return sharedPreferences.getBoolean( key , defaultValue ); } /** * 获取key相对应的value,如果不设默认参数,默认值为0 * * @param key * @return */ public int getInt( String key ) { return getInt( key , 0 ); } /** * 获取key相对应的value,如果不设默认参数,默认值为0 * * @param key * @return */ public int getInt( String key , int defaultValue ) { return sharedPreferences.getInt( key , defaultValue ); } /** * 获取key相对应的value,如果不设默认参数,默认值为0 * * @param key * @return */ public long getLong( String key ) { return getLong( key , 0L ); } /** * 获取key相对应的value,如果不设默认参数,默认值为0 * * @param key * @param defalutValue * @return */ public long getLong( String key , Long defaultValue ) { return sharedPreferences.getLong( key , defaultValue ); } /** * 获取key相对应的value,如果不设默认参数,默认值为0 * * @param key * @return */ public float getFloat( String key ) { return getFloat( key , 0f ); } /** * 获取key相对应的value,如果不设默认参数,默认值为0 * * @param key * @param defalutValue * @return */ public float getFloat( String key , Float defaultValue ) { return sharedPreferences.getFloat( key , defaultValue ); } /** 判断是否存在此字段 */ public boolean contains( String key ) { return sharedPreferences.contains( key ); } /** 判断是否存在此字段 */ public boolean has( String key ) { return sharedPreferences.contains( key ); } /** 删除sharedPreferences文件中对应的Key和value */ public boolean remove( String key ) { Editor editor = sharedPreferences.edit( ); editor.remove( key ); return editor.commit( ); }}
更多相关文章
- 获取手机基本信息的工具类
- android 获取屏幕是否被锁
- 单选菜单
- Android(安卓)使用Sharedpreference共享参数
- android abslistview gridview介绍
- ActivityManagerService中根据UID获取App包名
- 购物车(retrofit2+rxjava2+dagger+fresco+butterknife+xrecycler
- getExternalFilesDir()与getExternalStorageDirectory()区别
- Android中判断网络是否可用的代码分享