PreferencesUtils.java
import android.content.Context;
import android.content.SharedPreferences;
/**
* Used 临时存储数据操作类(全)
*/
public class PreferencesUtils {
public static String PREFERENCE_NAME = "why";
/**用户名的key值*/
public static String USERNAME = "username";
/**存储字符串*/
public static boolean putString(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
/**读取字符串*/
public static String getString(Context context, String key) {
return getString(context, key, null);
}
/**读取字符串(带默认值的)*/
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getString(key, defaultValue);
}
/**存储整型数字*/
public static boolean putInt(Context context, String key, int value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
return editor.commit();
}
/**读取整型数字*/
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
/**读取整型数字(带默认值的)*/
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getInt(key, defaultValue);
}
/**存储长整型数字*/
public static boolean putLong(Context context, String key, long value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong(key, value);
return editor.commit();
}
/**读取长整型数字*/
public static long getLong(Context context, String key) {
return getLong(context, key, 0xffffffff);
}
/**读取长整型数字(带默认值的)*/
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getLong(key, defaultValue);
}
/**存储Float数字*/
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**读取Float数字*/
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1.0f);
}
/**读取Float数字(带默认值的)*/
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getFloat(key, defaultValue);
}
/**存储boolean类型数据*/
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
/**读取boolean类型数据*/
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
/**读取boolean类型数据(带默认值的)*/
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getBoolean(key, defaultValue);
}
/**清除数据*/
public static boolean clearPreferences(Context context) {
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
return editor.commit();
}
}
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
* @param context
* @param key
* @param object
*/
public static void setParam(Context context , String key, Object object){
String type = object.getClass().getSimpleName();
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if("String".equals(type)){
editor.putString(key, (String)object);
}
else if("Integer".equals(type)){
editor.putInt(key, (Integer)object);
}
else if("Boolean".equals(type)){
editor.putBoolean(key, (Boolean)object);
}
else if("Float".equals(type)){
editor.putFloat(key, (Float)object);
}
else if("Long".equals(type)){
editor.putLong(key, (Long)object);
}
editor.commit();
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
* @param context
* @param key
* @param defaultObject
* @return
*/
public static Object getParam(Context context , String key, Object defaultObject){
String type = defaultObject.getClass().getSimpleName();
SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
if("String".equals(type)){
return sp.getString(key, (String)defaultObject);
}
else if("Integer".equals(type)){
return sp.getInt(key, (Integer)defaultObject);
}
else if("Boolean".equals(type)){
return sp.getBoolean(key, (Boolean)defaultObject);
}
else if("Float".equals(type)){
return sp.getFloat(key, (Float)defaultObject);
}
else if("Long".equals(type)){
return sp.getLong(key, (Long)defaultObject);
}
return null;
}
/**
* 清除所有数据
* @param context
*/
public static void clear(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear().commit();
}
/**
* 清除指定数据
* @param context
*/
public static void clearAll(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove("定义的键名");
editor.commit();
}
}
2.然后在需要保存数据的地方调用方法:
SharedPreferencesUtils.setParam(LoginActivity.this,“userId”,personModle.getResult().getUser_id());
3.接下来就是在需要这些数据的时候获取:
SharedPreferencesUtils.getParam(getActivity(),“userId”,"");
4.最后在需要清除数据的逻辑中加入下边的方法就可以了:
SharedPreferencesUtils.clear(getApplicationContext());
https://blog.youkuaiyun.com/androidsj/article/details/79796194
https://www.cnblogs.com/whycxb/p/7026666.html PreferencesUtils【SharedPreferences操作工具类】