首页 > 其他 > 详细

SharedPreferences工具类

时间:2015-10-29 23:09:43      阅读:257      评论:0      收藏:0      [点我收藏+]

不多说看!

 1 /**
 2  * SharedPreferenc工具类
 3  */
 4 public final class SpUtils {
 5 
 6     private static SharedPreferences sp;
 7 
 8     private static SpUtils instance = new SpUtils();
 9 
10     private SpUtils() {
11 
12     }
13 
14     /**
15      * 获取实例
16      */
17     public static SpUtils getInstance(Context context) {
18         if (sp == null) {
19             sp = context.getSharedPreferences("yyb", Context.MODE_PRIVATE);
20         }
21         return instance;
22     }
23 
24     /**
25      * 保存键值对
26      */
27     public void save(String key, Object value) {
28         Editor edit = sp.edit();
29         if (value instanceof String) {
30             edit.putString(key, (String) value);
31         } else if (value instanceof Integer) {
32             edit.putInt(key, (Integer) value);
33         } else if (value instanceof Boolean) {
34             edit.putBoolean(key, (Boolean) value);
35         }else if (value instanceof Long) {
36             edit.putLong(key, (Long) value);
37         }
38         edit.commit();
39     }
40 
41     public String getString(String key, String defValue) {
42 
43         return sp.getString(key, defValue);
44     }
45     
46     public int getInt(String key, Integer defValue) {
47         return sp.getInt(key, defValue);
48     }
49 
50     public Boolean getBoolean(String key, boolean defValue) {
51         return sp.getBoolean(key, defValue);
52     }
53     public Long getLong(String key, long defValue) {
54         return sp.getLong(key, defValue);
55     }
56 
57     public void remove(String key) {
58         Editor editor = sp.edit();
59         editor.remove(key);
60         editor.commit();
61     }
62 }

 

SharedPreferences工具类

原文:http://www.cnblogs.com/yangang2013/p/4921978.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!