hc
2023-11-07 f45e756958099c35d6afb746df1d40a1c6302cfc
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.rockchip.alexa.jacky.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.rockchip.alexa.jacky.app.BaseApplication;
 
 
/**
 * Created by Administrator on 2016/8/4.
 */
public class SharedPreference {
 
    private static final String PREF_NAME = "rk_echo";
    /**
     * 向SharedPreferences中写入int类型数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param value 值
     */
    public static void putInt(Context context, String name, String key, int value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putInt(key, value);
        sp.commit();
    }
    
    public static void putInt(Context context, String key, int value) {
        putInt(context, PREF_NAME, key, value);
    }
 
    public static void putInt(String key, int value) {
        putInt(BaseApplication.getInstance().getApplicationContext(), key, value);
    }
 
    /**
     * 向SharedPreferences中写入boolean类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param value 值
     */
    public static void putBoolean(Context context, String name, String key, boolean value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putBoolean(key, value);
        sp.commit();
    }
 
    public static void putBoolean(Context context, String key, boolean value) {
        putBoolean(context, PREF_NAME, key, value);
    }
 
    public static void putBoolean(String key, boolean value) {
        putBoolean(BaseApplication.getInstance().getApplicationContext(), key, value);
    }
 
    /**
     * 向SharedPreferences中写入String类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param value 值
     */
    public static void putString(Context context, String name, String key, String value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putString(key, value);
        sp.commit();
    }
 
    public static void putString(Context context, String key, String value) {
        putString(context, PREF_NAME, key, value);
    }
 
    public static void putString(String key, String value) {
        putString(BaseApplication.getInstance().getApplicationContext(), key, value);
    }
 
    /**
     * 向SharedPreferences中写入float类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param value 值
     */
    public static void putFloat(Context context, String name, String key, float value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putFloat(key, value);
        sp.commit();
    }
 
    public static void putFloat(Context context, String key, float value) {
        putFloat(context, PREF_NAME, key, value);
    }
 
    public static void putFloat(String key, float value) {
        putFloat(BaseApplication.getInstance().getApplicationContext(), key, value);
    }
 
    /**
     * 向SharedPreferences中写入long类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param value 值
     */
    public static void putLong(Context context, String name, String key, long value) {
        SharedPreferences.Editor sp = getEditor(context, name);
        sp.putLong(key, value);
        sp.commit();
    }
 
    public static void putLong(Context context, String key, long value) {
        putLong(context, PREF_NAME, key, value);
    }
 
    public static void putLong(String key, long value) {
        putLong(BaseApplication.getInstance().getApplicationContext(), key, value);
    }
 
    /**
     * 从SharedPreferences中读取int类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param defValue 如果读取不成功则使用默认值
     * @return 返回读取的值
     */
    public static int getInt(Context context, String name, String key, int defValue) {
        SharedPreferences sp = getPreferences(context, name);
        int value = sp.getInt(key, defValue);
        return value;
    }
 
    public static int getInt(Context context, String key, int defalue) {
        return getInt(context, PREF_NAME, key, defalue);
    }
 
    public static int getInt(String key, int defValue) {
        return getInt(BaseApplication.getInstance().getApplicationContext(), key, defValue);
    }
 
    /**
     * 从SharedPreferences中读取boolean类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param defValue 如果读取不成功则使用默认值
     * @return 返回读取的值
     */
    public static boolean getBoolean(Context context, String name, String key, boolean defValue) {
        SharedPreferences sp = getPreferences(context, name);
        boolean value = sp.getBoolean(key, defValue);
        return value;
    }
 
    public static boolean getBoolean(Context context, String key, boolean defalue) {
        return getBoolean(context, PREF_NAME, key, defalue);
    }
 
    public static boolean getBoolean(String key, boolean defValue) {
        return getBoolean(BaseApplication.getInstance().getApplicationContext(), key, defValue);
    }
 
    /**
     * 从SharedPreferences中读取String类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param defValue 如果读取不成功则使用默认值
     * @return 返回读取的值
     */
    public static String getString(Context context, String name, String key, String defValue) {
        SharedPreferences sp = getPreferences(context, name);
        String value = sp.getString(key, defValue);
        return value;
    }
 
    public static String getString(Context context, String key, String defalue) {
        return getString(context, PREF_NAME, key, defalue);
    }
 
    public static String getString(String key, String defValue) {
        return getString(BaseApplication.getInstance().getApplicationContext(), key, defValue);
    }
 
    /**
     * 从SharedPreferences中读取float类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param defValue 如果读取不成功则使用默认值
     * @return 返回读取的值
     */
    public static float getFloat(Context context, String name, String key, float defValue) {
        SharedPreferences sp = getPreferences(context, name);
        float value = sp.getFloat(key, defValue);
        return value;
    }
 
    public static float getFloat(Context context, String key, float defalue) {
        return getFloat(context, PREF_NAME, key, defalue);
    }
 
    public static float getFloat(String key, float defValue) {
        return getFloat(BaseApplication.getInstance().getApplicationContext(), key, defValue);
    }
 
    /**
     * 从SharedPreferences中读取long类型的数据
     *
     * @param context 上下文环境
     * @param name 对应的xml文件名称
     * @param key 键
     * @param defValue 如果读取不成功则使用默认值
     * @return 返回读取的值
     */
    public static long getLong(Context context, String name, String key, long defValue) {
        SharedPreferences sp = getPreferences(context, name);
        long value = sp.getLong(key, defValue);
        return value;
    }
 
    public static long getLong(Context context, String key, long defalue) {
        return getLong(context, PREF_NAME, key, defalue);
    }
 
    public static long getLong(String key, long defValue) {
        return getLong(BaseApplication.getInstance().getApplicationContext(), key, defValue);
    }
 
    private static SharedPreferences.Editor getEditor(Context context, String name) {
        return getPreferences(context, name).edit();
    }
 
    public static SharedPreferences.Editor getEditor(Context context) {
        return getEditor(context, PREF_NAME);
    }
 
    private static SharedPreferences getPreferences(Context context, String name) {
        return context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }
 
    private static SharedPreferences getPreferences(Context context) {
        return getPreferences(context, PREF_NAME);
    }
}