SharedPreferencesUtil.java
3.4 KB
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
package com.xdy.util;
import android.content.Context;
import android.content.SharedPreferences;
/**
* @author 蒋洪波
* @file SharedPreferencesUtil.java
* @brief SP工具
* @date 2015-6-10
* Copyright (c) 2015, 左键视觉[电子商务视觉完整解决方案服务商]
* All rights reserved.
*/
public class SharedPreferencesUtil {
private static String CONFIG = "config";
private static SharedPreferences sharedPreferences;
public static final String USERTOKEN = "usertoken";
public static final String USERID = "userId";
public static final String USERICON = "usericon";
public static final String USERNAME = "username";
public static final String USERINFO = "userInfo";
/**
* 删除key键的值
*
* @param context
* @param key
*/
public static void removeStringData(Context context, String key) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
sharedPreferences.edit().remove(key).commit();
}
// 存
public static synchronized void saveStringData(Context context, String key,
String value) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
sharedPreferences.edit().putString(key, value).commit();
}
// 存boolean
public static void saveBooleanData(Context context, String key,
boolean value) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
sharedPreferences.edit().putBoolean(key, value).commit();
}
// 取boolean
public static boolean getBooleanData(Context context, String key,
boolean defValue) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
return sharedPreferences.getBoolean(key, defValue);
}
// 取
public static synchronized String getStringData(Context context,
String key, String defValue) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
return sharedPreferences.getString(key, defValue);
}
// 删
public static boolean deleteStringData(Context context, String key) {
if (sharedPreferences == null) {
sharedPreferences = context.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
return sharedPreferences.edit().remove(key).commit();
}
/**
* 删除boolean值
*
* @param applicationContext
* @param string
*/
public static void removeBooleanData(Context applicationContext,
String string) {
if (sharedPreferences == null) {
sharedPreferences = applicationContext.getSharedPreferences(CONFIG,
Context.MODE_PRIVATE);
}
sharedPreferences.edit().remove(string).commit();
}
}