标签:
SharedPreferences
import android.content.Context; import android.content.SharedPreferences; import java.util.ArrayList; import java.util.List; /** * 键值对存储 * Created by load on 2015/10/21. */ public class SharePrefUtils { /** * 存储是否第一次运行 * * @param context * @param b */ public static void savaIsFirst(Context context, boolean b) { SharedPreferences preferences = context.getSharedPreferences("app", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("app_IsFirst", b); editor.commit(); } /** * 读取是否是第一次运行 * * @param context * @return */ public static boolean getIsFirst(Context context) { SharedPreferences preferences = context.getSharedPreferences("app", Context.MODE_PRIVATE); return preferences.getBoolean("app_IsFirst", true); } /** * 存储用户ID * * @param context * @param token */ public static void savaToken(Context context, String token) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("token", token); editor.commit(); } /** * 读取用户ID * * @param context * @return */ public static String getToken(Context context) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); return preferences.getString("token", null); } public static void savaTokenRef(Context context, String token) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("tokenRef", token); editor.commit(); } /** * 读取用户ID * * @param context * @return */ public static String getTokenRef(Context context) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); return preferences.getString("tokenRef", null); } /** * 存储搜索记录 */ public static void saveHistory(Context context, String histTxt) { if (isExitHistory(context, histTxt)) { return; } SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("history_" + (getHistNum(context) + 1), histTxt); editor.putInt("history_Num", getHistNum(context) + 1); editor.commit(); } /** * 删除历史记录 * * @param context */ public static void deleteHistory(Context context) { SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE); preferences.edit().clear().commit(); } private static boolean isExitHistory(Context context, String histTxt) { boolean b = false; for (int i = 0; i < getHistNum(context); i++) { if (histTxt.equals(getHistory(context).get(i))) { b = true; break; } } return b; } /** * 获取搜索记录 */ public static List<String> getHistory(Context context) { SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE); int historyNum = getHistNum(context); if (historyNum == 0) { return null; } else { List<String> list = new ArrayList<>(); for (int i = 0; i < historyNum; i++) { list.add(preferences.getString("history_" + (i + 1), "")); } return list; } } /** * 存储历史记录条数 * * @param context * @param num */ private static void saveHistNum(Context context, int num) { SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putInt("history_Num", num); editor.commit(); } /** * 获取历史记录条数 */ private static int getHistNum(Context context) { SharedPreferences preferences = context.getSharedPreferences("data", Context.MODE_PRIVATE); return preferences.getInt("history_Num", 0); } /** * 存储模块状态 * * @param context * @param isOpen */ public static void savaModuleStatue(Context context, Boolean isOpen) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("isOpen", isOpen); editor.commit(); } /** * 读取模块状态 * * @param context * @return */ public static Boolean getModuleStatue(Context context) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); return preferences.getBoolean("isOpen", false); } /** * 存储搜索状态 * * @param context * @param isSearch */ public static void savaSearchStatue(Context context, Boolean isSearch) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("isSearch", isSearch); editor.commit(); } /** * 读取搜索状态 * * @param context * @return */ public static Boolean getSearchStatue(Context context) { SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE); return preferences.getBoolean("isSearch", false); } }
标签:
原文地址:http://blog.csdn.net/qq_26296197/article/details/51321069