rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
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
package com.doumee.lib_coremodel.util;
 
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.text.TextUtils;
import android.view.WindowManager;
 
import com.google.gson.Gson;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by Administrator on 2016/10/14.
 */
 
public class SpUtil {
    private static final String FILE_NAME = "app_parameter";
    private static SharedPreferences sharedPreferences;
 
    public static void init(Context context){
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        }
    }
 
    private static SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }
 
    public static boolean remoreUser() {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.remove("user");
        return editor.commit();
    }
 
    public static <T> T getBean(String key,Class<T> cls) {
        String s = getSharedPreferences().getString(key, "");
        if (!TextUtils.isEmpty(s)) {
            return new Gson().fromJson(s, cls);
        }
        return null;
    }
 
    public static boolean saveBean(String key,Object labour) {
        String s = new Gson().toJson(labour);
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, s);
        return editor.commit();
    }
 
    public static String getString(String key){
        return getSharedPreferences().getString(key, "");
    }
 
    /**
     * 获取String
     *
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(String key, String defValue) {
        return getSharedPreferences().getString(key, defValue);
    }
 
    public static int getInt(String key, int defValue){
        return getSharedPreferences().getInt(key, defValue);
    }
 
    public static void setInt(String key, int defValue){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putInt(key, defValue);
        editor.commit();
    }
 
    /**
     * 添加String
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }
 
    public static void saveString(String key, String value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }
 
    public static void saveInt(String key, int value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
    }
 
    public static int getInt(String key){
        return getSharedPreferences().getInt(key, 0);
    }
 
    public static boolean getBoolean(String key){
        return getSharedPreferences().getBoolean(key, false);
    }
 
    public static void saveBoolean(String key, boolean value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }
 
    public static boolean remoreString(String key) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.remove(key);
        return editor.commit();
    }
 
    public static boolean haveKey(String key){
        return getSharedPreferences().contains(key);
    }
 
    //是否是游客登陆
    public static boolean isVisitor(){
        return getSharedPreferences().getBoolean("isVisitor",false);
    }
 
    //是否是游客登陆
    public static void saveVisitor(boolean is){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean("isVisitor", is);
        editor.commit();
    }
 
    /**
     * 屏幕宽高
     */
    private static  int WIDTH=0;
    private static  int HEIGHT=0;
 
    public static int getWIDTH(){
        if(WIDTH==0){
            WIDTH= getInt("WIDTH",0);
        }
        return WIDTH;
    }
    public static void setWIDTH(int width){
        if(width!=0){
            saveInt("WIDTH",width);
            WIDTH=width;
        }
    }
 
    public static int getHEIGHT(){
        if(HEIGHT==0){
            HEIGHT= getInt("HEIGHT",0);
        }
        return HEIGHT;
    }
 
    public static void setHEIGHT(int height){
        if(height!=0){
            saveInt("HEIGHT",height);
            HEIGHT=height;
        }
    }
 
    public static void initWH(Activity activity){
        WindowManager wm = activity.getWindowManager();
        Point point = new Point();
        wm.getDefaultDisplay().getSize(point);
        setWIDTH(point.x);
        setHEIGHT(point.y);
    }
 
    public static List<SPBean> getMatchingStrings(String pattern) {
        List<String> keys = new ArrayList<>(sharedPreferences.getAll().keySet());
        List<SPBean> beans = new ArrayList<>();
        for (String key : keys) {
            if (key.contains(pattern)) {
                beans.add(new SPBean(key,getString(key)));
            }
        }
        return beans;
    }
 
    public static class SPBean{
        private String key;
        private String value;
        private int pos;
 
        public SPBean(String key, String value) {
            this.key = key;
            this.value = value;
        }
 
        public String getKey() {
            return key;
        }
 
        public void setKey(String key) {
            this.key = key;
        }
 
        public String getValue() {
            return value;
        }
 
        public void setValue(String value) {
            this.value = value;
        }
 
        public int getPos() {
            return pos;
        }
 
        public void setPos(int pos) {
            this.pos = pos;
        }
    }
}