6、通用类Utils的常用函数

来源:互联网 发布:生死狙击球棒数据变异 编辑:程序博客网 时间:2024/05/10 21:15
packagecom.example.wechat.common;

importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.preference.PreferenceManager;
importandroid.util.Log;

/**
* Created by sing on 2016/1/26.
*/
public classUtils {

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

private static final SharedPreferences getSharedPreference(Context context) {
returnPreferenceManager.getDefaultSharedPreferences(context);
}

/**
* 移除SharedPreference
*
*@paramcontext
*@paramkey
*/
public static final void RemoveValue(Context context, String key) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.remove(key);
booleanresult = editor.commit();
if(!result) {
Log.e("移除Shared","save "+ key +" failed");
}
}

/**
* 获取SharedPreference 值
*
*@paramcontext
*@paramkey
*@return
*/
public static final String getValue(Context context, String key) {
returngetSharedPreference(context).getString(key,"");
}

public static finalBoolean getBooleanValue(Context context, String key) {
returngetSharedPreference(context).getBoolean(key,false);
}

public static final voidputBooleanValue(Context context, String key,
booleanbl) {
SharedPreferences.Editor edit =getSharedPreference(context).edit();
edit.putBoolean(key, bl);
edit.commit();
}

public static final intgetIntValue(Context context, String key) {
returngetSharedPreference(context).getInt(key,0);
}

public static final longgetLongValue(Context context, String key,
longdefault_data) {
returngetSharedPreference(context).getLong(key, default_data);
}

public static final booleanputLongValue(Context context, String key,
Long value) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.putLong(key, value);
returneditor.commit();
}

public static finalBoolean hasValue(Context context, String key) {
returngetSharedPreference(context).contains(key);
}

/**
* 设置SharedPreference 值
*
*@paramcontext
*@paramkey
*@paramvalue
*/
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);
booleanresult = editor.commit();
if(!result) {
return false;
}
return true;
}

/**
* 设置SharedPreference 值
*
*@paramcontext
*@paramkey
*@paramvalue
*/
public static final boolean putIntValue(Context context, String key,
intvalue) {
SharedPreferences.Editor editor =getSharedPreference(context).edit();
editor.putInt(key, value);
booleanresult = editor.commit();
if(!result) {
return false;
}
return true;
}

/***************************************************************************/
}


0 0
原创粉丝点击