forked from azhon/MyUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharePreUtils.java
More file actions
162 lines (144 loc) · 4.6 KB
/
Copy pathSharePreUtils.java
File metadata and controls
162 lines (144 loc) · 4.6 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
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
package cn.hjtech.pigeon.common.utils;
import android.content.Context;
import android.content.SharedPreferences;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/*
* 文件名: SharePreUtils
* 创建时间: 2017/05/24 on 11:58
* 描述: SharedPreferences存取工具类
*/
public class SharePreUtils {
//配置文件,文件名
public static final String SHARE_NAME = "config";
/**
* 万能put
*
* @param mContext 上下文
* @param key 键
* @param values 值
*/
public static void put(Context mContext, String key, Object values) {
if (values instanceof String) {
putString(mContext, key, (String) values);
} else if (values instanceof Integer) {
putInt(mContext, key, (Integer) values);
} else if (values instanceof Boolean) {
putBoolean(mContext, key, (Boolean) values);
} else {
//除去上面三个类型,都存储为String
putString(mContext, key, String.valueOf(values));
}
}
/**
* 存字符串
*
* @param mContext
* @param key
* @param values
*/
public static void putString(Context mContext, String key, String values) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
sp.edit().putString(key, values).commit();
}
/**
* 取字符串
*
* @param mContext 上下文
* @param key 键
* @param values 默认值
* @return 取出的值
*/
public static String getString(Context mContext, String key, String values) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
return sp.getString(key, values);
}
/**
* 存布尔值
*
* @param mContext 上下文
* @param key 键
* @param values 值
*/
public static void putBoolean(Context mContext, String key, boolean values) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
sp.edit().putBoolean(key, values).commit();
}
/**
* 取布尔值
*
* @param mContext 上下文
* @param key 键
* @param values 默认值
* @return true/false
*/
public static boolean getBoolean(Context mContext, String key, boolean values) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
return sp.getBoolean(key, values);
}
/**
* 存int值
*
* @param mContext 上下文
* @param key 键
* @param values 值
*/
public static void putInt(Context mContext, String key, int values) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
sp.edit().putInt(key, values).commit();
}
/**
* 取int值
*
* @param mContext 上下文
* @param key 键
* @param values 默认值
* @return
*/
public static int getInt(Context mContext, String key, int values) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
return sp.getInt(key, values);
}
/**
* 删除一条字段
*
* @param mContext 上下文
* @param key 键
*/
public static void deleShare(Context mContext, String key) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
//单个清理
sp.edit().remove(key).commit();
}
/**
* 删除全部数据
*
* @param mContext 上下文
*/
public static void deleShareAll(Context mContext) {
SharedPreferences sp = mContext.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE);
//全部清理
sp.edit().clear().commit();
}
/**
* 查看SharedPreferences的内容
*/
public static String lookSharePre(Context context) {
try {
FileInputStream stream = new FileInputStream(new File("/data/data/" + context.getPackageName() + "/shared_prefs", "config.xml"));
BufferedReader bff = new BufferedReader(new InputStreamReader(stream));
String line;
StringBuilder sb = new StringBuilder();
while ((line = bff.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "未找到当前配置文件!";
}
}