您的位置:首页 > 其它

6、通用类Utils的常用函数

2016-01-26 16:22 295 查看
package
com.example.wechat.common;

import
android.content.Context;

import
android.content.SharedPreferences;

import
android.preference.PreferenceManager;

import
android.util.Log;

/**

* Created by sing on 2016/1/26.

*/

public class
Utils {

/***************************************************************************/

private
static final
SharedPreferences getSharedPreference(Context context) {

return
PreferenceManager.getDefaultSharedPreferences(context);

}

/**

* 移除SharedPreference

*

*
@param
context

*
@param
key

*/

public
static final void
RemoveValue(Context context, String key) {

SharedPreferences.Editor editor =
getSharedPreference(context).edit();

editor.remove(key);

boolean
result = editor.commit();

if
(!result) {

Log.e("移除Shared",
"save "
+ key +
" failed");

}

}

/**

* 获取SharedPreference 值

*

*
@param
context

*
@param
key

*
@return

*/

public
static final
String getValue(Context context, String key) {

return
getSharedPreference(context).getString(key,
"");

}

public static final
Boolean getBooleanValue(Context context, String key) {

return
getSharedPreference(context).getBoolean(key,
false);

}

public static final void
putBooleanValue(Context context, String key,

boolean
bl) {

SharedPreferences.Editor edit =
getSharedPreference(context).edit();

edit.putBoolean(key, bl);

edit.commit();

}

public static final int
getIntValue(Context context, String key) {

return
getSharedPreference(context).getInt(key,
0);

}

public static final long
getLongValue(Context context, String key,

long
default_data) {

return
getSharedPreference(context).getLong(key,
default_data);

}

public static final boolean
putLongValue(Context context, String key,

Long value) {

SharedPreferences.Editor editor =
getSharedPreference(context).edit();

editor.putLong(key, value);

return
editor.commit();

}

public static final
Boolean hasValue(Context context, String key) {

return
getSharedPreference(context).contains(key);

}

/**

* 设置SharedPreference 值

*

*
@param
context

*
@param
key

*
@param
value

*/

public
static final boolean
putValue(Context context, String key,

String value) {

value = value ==
null
?
""
: value;

SharedPreferences.Editor editor =
getSharedPreference(context).edit();

editor.putString(key, value);

boolean
result = editor.commit();

if
(!result) {

return false;

}

return true;

}

/**

* 设置SharedPreference 值

*

*
@param
context

*
@param
key

*
@param
value

*/

public
static final boolean
putIntValue(Context context, String key,

int
value) {

SharedPreferences.Editor editor =
getSharedPreference(context).edit();

editor.putInt(key, value);

boolean
result = editor.commit();

if
(!result) {

return false;

}

return true;

}

/***************************************************************************/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: