您的位置:首页 > Web前端

SharedPreferencesUtil

2017-07-12 14:56 316 查看
/**
*配置存储
*/
public class SharedPreferencesUtil {

private final static String NAME = "settings";

/**
* put int value
* @param context
* @param key
* @param value
*/
public static void putIntValue(Context context,String key,int value){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor edit = sharedPreferences.edit();
edit.putInt(key, value);
edit.commit();
}

/**
* put float value
* @param context
* @param key
* @param value
*/
public static void putFloatValue(Context context,String key,float value){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor edit = sharedPreferences.edit();
edit.putFloat(key, value);
edit.commit();
}

/**
* put boolean value
* @param context
* @param key
* @param value
*/
public static void putBooleanValue(Context context,String key,boolean value){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor edit = sharedPreferences.edit();
edit.putBoolean(key, value);
edit.commit();
}

/**
* put long value
* @param context
* @param key
* @param value
*/
public static void putLongValue(Context context,String key,long value){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor edit = sharedPreferences.edit();
edit.putLong(key, value);
edit.commit();
}

/**
* put String value
* @param context
* @param key
* @param value
*/
public static void setStringValue(Context context,String key,String value){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
Editor edit = sharedPreferences.edit();
edit.putString(key, value);
edit.commit();
}

public static int getIntValue(Context context,String key,int defValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, defValue);
}

public static float getFloatValue(Context context,String key,float defValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
return sharedPreferences.getFloat(key, defValue);
}

public static long getLongValue(Context context,String key,long defValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
return sharedPreferences.getLong(key, defValue);
}

public static String getStringValue(Context context,String key,String defValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
return sharedPreferences.getString(key, defValue);
}

public static boolean getBooleanValue(Context context,String key,boolean defValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, defValue);
}

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