lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cn.sinata.xldutils.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
import cn.sinata.xldutils.xldUtils;
import java.util.Map;
 
/**
 *
 * SharedPreferences工具类
 */
public class SPUtils {
    static SharedPreferences sp;
 
    public static void init(Context context){
        if (sp==null) {
            String spName;
            if (StringUtils.isEmpty(xldUtils.SPNAME)){
                spName = context.getPackageName();
            }else {
                spName = xldUtils.SPNAME;
            }
            sp=context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        }
    }
 
    /**
     * 保存SharedPreferences数据
     * @param map 键值对,值必须是int,float,boolean,string,long类型,其余类型不支持
     */
    public static synchronized void save(Map<String,Object> map){
        if (sp!=null) {
            SharedPreferences.Editor editor = sp.edit();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
//            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                Object o = entry.getValue();
                if (o instanceof String) {
                    editor.putString(entry.getKey(), (String) o);
                } else if (o instanceof Integer) {
                    editor.putInt(entry.getKey(), (Integer) o);
                } else if (o instanceof Long) {
                    editor.putLong(entry.getKey(), (Long) o);
                } else if (o instanceof Boolean) {
                    editor.putBoolean(entry.getKey(), (Boolean) o);
                } else if (o instanceof Float) {
                    editor.putFloat(entry.getKey(), (Float) o);
                } else {
                    //不支持的类型,不做操作
                    new Throwable("不支持类型").printStackTrace();
                }
            }
            editor.apply();
        }
    }
 
    /**
     * 保存String型数据
     * @param key 键
     * @param value 值
     */
    public static synchronized void save(String key,String value){
        if (sp!=null) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, value).apply();
        }
    }
 
    /**
     * 保存Int型数据
     * @param key 键
     * @param value 值
     */
    public static synchronized void save(String key,int value){
        if (sp!=null) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt(key, value).apply();
        }
    }
 
    /**
     * 保存Boolean型数据
     * @param key 键
     * @param value 值
     */
    public static synchronized void save(String key,boolean value){
        if (sp!=null) {
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean(key, value).apply();
        }
    }
 
    /**
     * 获取存储的字符型数据
     * @param key 键
     * @return value
     */
    public static String getString(String key){
 
        return sp==null?"":sp.getString(key,"");
    }
 
    /**
     * 获取存储的布尔型数据
     * @param key 键
     * @return value
     */
    public static boolean getBoolean(String key){
        return getBoolean(key,false);
    }
 
    /**
     * 获取存储的int型数据
     * @param key 键
     * @return value 默认-1
     */
    public static int getInt(String key){
        return sp!=null?sp.getInt(key,-1):-1;
    }
 
    /**
     * 获取存储的布尔型数据
     * @param key 键
     * @param def 默认值
     * @return value
     */
    public static boolean getBoolean(String key,boolean def){
        return sp!=null&&sp.getBoolean(key,def);
    }
 
    /**
     * 删除对应key的SharedPreferences数据
     * @param key 键
     */
    public static synchronized void remove(String key){
        if (sp!=null) {
            sp.edit().remove(key).apply();
        }
    }
 
    /**
     * 删除对应key的SharedPreferences数据
     * @param keys 需删除的键的集合
     */
    public static synchronized void remove(String[] keys){
        if (sp!=null) {
            for (String key : keys) {
                sp.edit().remove(key).apply();
            }
        }
    }
 
    /**
     * 清空所有SharedPreferences数据
     */
    public static void clear(){
        if (sp!=null) {
            sp.edit().clear().apply();
        }
    }
 
}