相信的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数据的时候,就可以直接通过工具类的set和get方法来完成,类似JavaBean,这样使用起来就比较方便,快捷(建议项目中使用次数比较多使用)。好了,直接看看这段简单的代码吧:
public class SharePreferenceUtil { private SharedPreferences sp; private SharedPreferences.Editor editor; public SharePreferenceUtil(Context context, String file) { sp = context.getSharedPreferences(file, context.MODE_PRIVATE); editor = sp.edit(); } // 用户的密码 public void setPasswd(String passwd) { editor.putString("passwd", passwd); editor.commit(); } public String getPasswd() { return sp.getString("passwd", ""); } // 用户的id,即QQ号 public void setId(String id) { editor.putString("id", id); editor.commit(); } public String getId() { return sp.getString("id", ""); } // 用户的昵称 public String getName() { return sp.getString("name", ""); } public void setName(String name) { editor.putString("name", name); editor.commit(); } // 用户的邮箱 public String getEmail() { return sp.getString("email", ""); } public void setEmail(String email) { editor.putString("email", email); editor.commit(); } // 用户自己的头像 public Integer getImg() { return sp.getInt("img", 0); } public void setImg(int i) { editor.putInt("img", i); editor.commit(); } // ip public void setIp(String ip) { editor.putString("ip", ip); editor.commit(); } public String getIp() { return sp.getString("ip", Constants.SERVER_IP); } // 端口 public void setPort(int port) { editor.putInt("port", port); editor.commit(); } public int getPort() { return sp.getInt("port", Constants.SERVER_PORT); } // 是否在后台运行标记 public void setIsStart(boolean isStart) { editor.putBoolean("isStart", isStart); editor.commit(); } public boolean getIsStart() { return sp.getBoolean("isStart", false); } // 是否第一次运行本应用 public void setIsFirst(boolean isFirst) { editor.putBoolean("isFirst", isFirst); editor.commit(); } public boolean getisFirst() { return sp.getBoolean("isFirst", true); }}
第二种方法是更加简单方便:取值时只用传入context和对应的key,就能获取到对应的value;设值时,也是传入context和对应key和value即可,非常方便实用。
public class PreferenceUtils { public static String getPrefString(Context context, String key, final String defaultValue) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); return settings.getString(key, defaultValue); } public static void setPrefString(Context context, final String key, final String value) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); settings.edit().putString(key, value).commit(); } public static boolean getPrefBoolean(Context context, final String key, final boolean defaultValue) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); return settings.getBoolean(key, defaultValue); } public static boolean hasKey(Context context, final String key) { return PreferenceManager.getDefaultSharedPreferences(context).contains( key); } public static void setPrefBoolean(Context context, final String key, final boolean value) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); settings.edit().putBoolean(key, value).commit(); } public static void setPrefInt(Context context, final String key, final int value) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); settings.edit().putInt(key, value).commit(); } public static int getPrefInt(Context context, final String key, final int defaultValue) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); return settings.getInt(key, defaultValue); } public static void setPrefFloat(Context context, final String key, final float value) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); settings.edit().putFloat(key, value).commit(); } public static float getPrefFloat(Context context, final String key, final float defaultValue) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); return settings.getFloat(key, defaultValue); } public static void setSettingLong(Context context, final String key, final long value) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); settings.edit().putLong(key, value).commit(); } public static long getPrefLong(Context context, final String key, final long defaultValue) { final SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); return settings.getLong(key, defaultValue); } public static void clearPreference(Context context, final SharedPreferences p) { final Editor editor = p.edit(); editor.clear(); editor.commit(); }}