package com.thorn.milk.storage;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thorn.milk.app.Milk;
/**
* Created by pengj on 2018-7-19.
* Github https://github.com/ThornFUN
* Function:
*/
public class MilkSharedPreference {
/**
* 提示:
* Activity.getPreferences(int mode)生成 Activity名.xml 用于Activity内部存储
* PreferenceManager.getDefaultSharedPreferences(Context)生成 包名_preferences.xml
* Context.getSharedPreferences(String name,int mode)生成name.xml
*/
private static final SharedPreferences PREFERENCES =
PreferenceManager.getDefaultSharedPreferences(Milk.getApplicationContext());
private static final String APP_PREFERENCES_KEY = "profile";
private static SharedPreferences getAppPreference() {
return PREFERENCES;
}
public static String getAppProfile() {
return getAppPreference().getString(APP_PREFERENCES_KEY, null);
}
public static void setAppProfile(String val) {
getAppPreference()
.edit()
.putString(APP_PREFERENCES_KEY, val)
.apply();
}
public static JSONObject getAppProfileJson() {
final String profile = getAppProfile();
return JSON.parseObject(profile);
}
public static void removeAppProfile() {
getAppPreference()
.edit()
.remove(APP_PREFERENCES_KEY)
.apply();
}
public static void clearAppPreferences() {
getAppPreference()
.edit()
.clear()
.apply();
}
public static void setAppFlag(String key, boolean flag) {
getAppPreference()
.edit()
.putBoolean(key, flag)
.apply();
}
public static boolean getAppFlag(String key) {
return getAppPreference()
.getBoolean(key, false);
}
public static void addCustomAppProfile(String key, String val) {
getAppPreference()
.edit()
.putString(key, val)
.apply();
}
public static String getCustomAppProfile(String key) {
return getAppPreference().getString(key, "");
}
}
SharedPreference 项目中的标准写法
猜你喜欢
转载自blog.csdn.net/baidu_33221362/article/details/81119144
今日推荐
周排行