Android 工具类(Util) 之 SharedPreferences
新建类名SpfUtil
public static void saveStringSpf(Context context, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Preference", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String getStringSpf(Context context, String key,String defValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Preference", Context.MODE_PRIVATE);
return sharedPreferences.getString(key, defValus);
}
public static void saveIntSpf(Context context, String key, int value) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Preference", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
public static int getIntSpf(Context context, String key,int defValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Preference", Context.MODE_PRIVATE);
return sharedPreferences.getInt(key, defValus);
}
public static void saveBooleanSpf(Context context, String key, boolean value) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Preference", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
public static boolean getBooleanSpf(Context context, String key,boolean defValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("Preference", Context.MODE_PRIVATE);
return sharedPreferences.getBoolean(key, false);
}