25.Android 轻缓存工具SharedPreferencesUtil
public class SharedPreferencesUtil {
public static final String TAG = "SharedPreferencesUtils" ;
/**
* --> String <--
* Use SharedPreferences save the data
* SharedPreferences保存数据
*
* @param context
* @param xmlName
* @param key
* @param value
*/
public static void save (Context context, String xmlName, String key,
String value) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
if ((value != null ) && !"" .equals(value.trim())) {
editor.putString(key, value);
editor.apply();
} else {
Log.e(SharedPreferencesUtil.TAG, "The value parameter is invalid" );
}
}
/**
* --> int <--
* Use SharedPreferences save the data
* SharedPreferences保存数据
*
* @param context
* @param xmlName
* @param key
* @param value
*/
public static void save (Context context, String xmlName, String key,
int value) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
editor.apply();
}
/**
* --> int <--
* Use SharedPreferences save the data
* SharedPreferences保存数据
*
* @param context
* @param xmlName
* @param key
* @param value
*/
public static void save (Context context, String xmlName, String key,
long value) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong(key, value);
editor.apply();
}
/**
* --> float <--
* Use SharedPreferences save the data
* SharedPreferences保存数据
*
* @param context
* @param xmlName
* @param key
* @param value
*/
public static void save (Context context, String xmlName, String key,
float value) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat(key, value);
editor.apply();
}
/**
* --> String <--
* Use SharedPreferences load the data
* SharedPreferences读取数据
*
* @param context
* @param xmlName
* @param key
* @return
*/
public static String loadString (Context context, String xmlName, String key) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
return preferences.getString(key, null );
}
/**
* --> int <--
* Use SharedPreferences load the data
* SharedPreferences读取数据
*
* @param context
* @param xmlName
* @param key
* @return
*/
public static int loadInt (Context context, String xmlName, String key) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
return preferences.getInt(key, 0 );
}
/**
* --> long <--
* Use SharedPreferences load the data
* SharedPreferences读取数据
*
* @param context
* @param xmlName
* @param key
* @return
*/
public static long loadLong (Context context, String xmlName, String key) {
SharedPreferences preferences = context.getSharedPreferences(xmlName,
Context.MODE_PRIVATE);
return preferences.getLong(key, 0 );
}
}