android开发全局shared,android中对SharedPreference实现简单的封装

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( );

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值