-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedPreferencesUtils.java
More file actions
138 lines (113 loc) · 3.26 KB
/
Copy pathSharedPreferencesUtils.java
File metadata and controls
138 lines (113 loc) · 3.26 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
package com.example.odm.securitydetectionapp.util;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.odm.securitydetectionapp.application.SecurityDetectionAPP;
import com.orhanobut.logger.Logger;
/**
* @author: ODM
* @date: 2019/7/28
*/
public class SharedPreferencesUtils {
private SharedPreferences share;
private SharedPreferences.Editor editor;
private String SHARED_NAME = "spname";//sp的文件名 自定义
private SharedPreferencesUtils() {
share = SecurityDetectionAPP.getContext().getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
editor = share.edit();
}
/**
* 单例模式双重检查锁定
*/
private static SharedPreferencesUtils instance;
public static SharedPreferencesUtils getInstance() {
if (instance == null) {
synchronized (SharedPreferencesUtils.class) {
if (instance == null) {
instance = new SharedPreferencesUtils();
}
}
}
return instance;
}
/**
* ------- Int ---------
*/
public void putInt(String spName, int value) {
editor.putInt(spName, value);
editor.commit();
}
public int getInt(String spName, int defaultvalue) {
return share.getInt(spName, defaultvalue);
}
/**
* ------- String ---------
*/
public void putString(String spName, String value) {
editor.putString(spName, value);
Logger.d("存储的value" + value);
editor.commit();
}
public String getString(String spName, String defaultvalue) {
return share.getString(spName, defaultvalue);
}
public String getString(String spName) {
return share.getString(spName, "");
}
/**
* ------- boolean ---------
*/
public void putBoolean(String key, boolean value) {
editor.putBoolean(key, value);
editor.commit();
}
public boolean getBoolean(String key, boolean defValue) {
return share.getBoolean(key, defValue);
}
/**
* ------- float ---------
*/
public void putFloat(String key, float value) {
editor.putFloat(key, value);
editor.commit();
}
public float getFloat(String key, float defValue) {
return share.getFloat(key, defValue);
}
/**
* ------- long ---------
*/
public void putLong(String key, long value) {
editor.putLong(key, value);
editor.commit();
}
public long getLong(String key, long defValue) {
return share.getLong(key, defValue);
}
/**
* 清空SP里所有数据 谨慎调用
*/
public void clear() {
editor.clear();//清空
editor.commit();//提交
}
/**
* 删除SP里指定key对应的数据项
*
* @param key
*/
public void remove(String key) {
editor.remove(key);//删除掉指定的值
editor.commit();//提交
}
/**
* 查看sp文件里面是否存在此 key
*
* @param key
* @return
*/
public boolean contains(String key) {
return share.contains(key);
}
// 所有的Key 以为常量的形式保存在此类里面
public static final String WEBSOCK = "websocket";
}