开发中遇到的 基于sharedpreference对本地信息的处理
代码部分:
package pro_util; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; /** * Created by 小林哥 on 2016/10/14. * 用于将一些信息保存在本地的xml文件中 */ public class ShareUtil { public static SharedPreferences getSharedPreferences(Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); return sp; } public static void save(String key, Object value, Context context) { SharedPreferences.Editor e = getSharedPreferences(context).edit(); if (value instanceof String) { e.putString(key, (String) value); } else if (value instanceof Integer) { e.putInt(key, (Integer) value); } else if (value instanceof Boolean) { e.putBoolean(key, (Boolean) value); } else if (value instanceof Long) { e.putLong(key, (Long) value); } e.commit(); } public static String getName(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getString("name", "username"); } /** * 获取是否第一次登录 * * @param context * @return */ public static Boolean getIsFrist(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getBoolean("isFirst",true); } //获取用户名 public static String getAccount(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getString("Account",null); } //获取密码 public static String getPassword(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getString("passWord",null); } //移除一些设定的属性 public static void remove(String key,Context context){ SharedPreferences sp = getSharedPreferences(context); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); editor.commit(); } public static boolean isAaronLiInfo(Context context){ SharedPreferences sp = getSharedPreferences(context); return sp.getBoolean("isAaronLiInfo",false); } /** * 勿扰模式 获得开始小时 * * @param context * @return */ public static int getBeginHour(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getInt("beginHour",00); } /** * 勿扰模式 获得开始分钟 * * @param context * @return */ public static int getBeginMin(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getInt("beginMin",00); } /** * 勿扰模式 获得结束小时 * * @param context * @return */ public static int getEndHour(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getInt("endHour",00); } /** * 勿扰模式 获得结束分钟 * * @param context * @return */ public static int getEndMin(Context context) { SharedPreferences sp = getSharedPreferences(context); return sp.getInt("endMin",00); } /** * 是否开启声音 * @param context * @return */ public static boolean openVoice(Context context){ SharedPreferences sp = getSharedPreferences(context); return sp.getBoolean("voice",false); } /** *是否开启震动 * @param context * @return */ public static boolean openVibrate(Context context){ SharedPreferences sp = getSharedPreferences(context); return sp.getBoolean("vibration",false); } /** *是否开启通知详情 * @param context * @return */ public static boolean openNoticeInfo(Context context){ SharedPreferences sp = getSharedPreferences(context); return sp.getBoolean("flags",false); } }