您的位置:首页 > 移动开发 > Android开发

android 经常用到的一些工具类--SharedPreferencesUtils

2017-03-28 16:49 567 查看

趁现在还没找到工作,把我经常要用的东西整理一下放到blog上,免得以后还要在项目里到处找

SharedPreferencesUtils工具类

import android.content.Context;

import android.content.SharedPreferences;

/**

* author ${吴心良}

* data: 2017/2/16.

*/

public class SharedPreferencesUtils {

private static final String PRE_NAME = "config";
private static SharedPreferences sp;

/**
* 设置 boolean 值
* @param context
* @param key
* @param value
*/
public static void setBooleanValue(Context context, String key, boolean value){
if (sp == null) {
sp = context.getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
}
sp.edit().putBoolean(key, value).commit();
}
/**
* 返回 boolean 值
* @param context
* @param key
* @param defaultValue
* @return
*/
public static boolean getBooleanValue(Context context, String key, boolean defaultValue){
if (sp == null) {
sp = context.getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
}
return sp.getBoolean(key, defaultValue);
}
/**
* 设置 String 值
* @param context
* @param key
* @param
*/
public static void setStringValue(Context context, String key, String Value){
if (sp == null) {
sp = context.getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
}
sp.edit().putString(key, Value).commit();
}
/**
* 返回 String 值
* @param context
* @param key
* @param defaultValue
* @return
*/
public static String getStringValue(Context context, String key, String defaultValue){
if (sp == null) {
sp = context.getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
}
return sp.getString(key, defaultValue);
}

/**
* 设置Float值
* @param context
* @param key
* @param defaultValue
*/
public static void setFloatValue(Context context, String key, float defaultValue){
if (sp == null) {
sp = context.getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
}
sp.edit().putFloat(key, defaultValue).commit();
}

/**
* 返回Float值
* @param context
* @param key
* @param defaultValue
* @return
*/
public static float getFloatValue(Context context, String key, float defaultValue){
if (sp == null) {
sp = context.getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
}
return sp.getFloat(key, defaultValue);
}


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