Shared存储工具
import android.content.Context;
import android.content.SharedPreferences;
public class SharePreUtil {
public static String sharePreference="YOUR TAG";
public static String getStringValue(Context context,String key,String defaultValue) {
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
String result=share.getString(key, null);
if(result==null)return defaultValue;
return result;
}
public static boolean setStringValue(Context context,String key,String value) {
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
SharedPreferences.Editor edit= share.edit();
edit.putString(key, value);
return edit.commit();
}
public static boolean setBoolValue(Context context,String key,boolean value){
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
SharedPreferences.Editor edit= share.edit();
edit.putBoolean(key, value);
return edit.commit();
}
public static boolean getBoolValue(Context context,String key,boolean defaultValue) {
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
return share.getBoolean(key, defaultValue);
}
public static boolean setIntValue(Context context,String key,int value){
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
SharedPreferences.Editor edit= share.edit();
edit.putInt(key, value);
return edit.commit();
}
public static int getIntValue(Context context,String key,int defaultValue) {
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
return share.getInt(key, defaultValue);
}
public static boolean setFloatValue(Context context,String key,float value){
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
SharedPreferences.Editor edit= share.edit();
edit.putFloat(key, value);
return edit.commit();
}
public static float getFloatValue(Context context,String key,float defaultValue) {
SharedPreferences share =context.getSharedPreferences(sharePreference, Context.MODE_PRIVATE);
return share.getFloat(key, defaultValue);
}
}