Android-SharePreference工具类实现

个人总结的一些小知识点,各位有用就拿去,有更好的建议请指教,谢谢~

Android-SharePreference工具类实现:

封装SharePreference工具,用于记录一些系统数据和设置参数,目前只实现了boolean类型的数据读写。

代码如下:

 1 import android.content.Context;
 2 import android.content.SharedPreferences;
 3 
 4 /**
 5  * SharePreference封装
 6  * 
 7  * @author Kevin
 8  * 
 9  */
10 public class PrefUtils {
11 
12     public static final String PREF_NAME = "config";
13 
14     public static boolean getBoolean(Context ctx, String key,
15             boolean defaultValue) {
16         SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
17                 Context.MODE_PRIVATE);
18         return sp.getBoolean(key, defaultValue);
19     }
20 
21     public static void setBoolean(Context ctx, String key, boolean value) {
22         SharedPreferences sp = ctx.getSharedPreferences(PREF_NAME,
23                 Context.MODE_PRIVATE);
24         sp.edit().putBoolean(key, value).commit();
25     }
26 }
原文地址:https://www.cnblogs.com/lude313/p/4855656.html