左晓为主开发手持机充值管理机
zuoxiao
2024-07-29 fc1ec55e6ad56dc92737657750bcca7ed49f53eb
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
/**
 * Copyright (C), 2014-2017, Zhengzhou IMAN Technology Development. Co., Ltd
 * Company: ImanSoft( http://www.imansoft.cn/ )
 */
package com.dayu.qiheonlinelibrary.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Base64;
 
import java.util.Map;
import java.util.Set;
 
public class SharedPreferencesHelper {
    /**
     *
     */
    private SharedPreferences sharedPreferences;
 
    //
    public static final String name_file = "wsGameSDK_helper";
 
    /**
     * 构造函数
     *
     * @param context
     */
    private SharedPreferencesHelper(Context context, String name, int mode) {
        super();
        this.sharedPreferences = context.getSharedPreferences(name, mode);
    }
 
    public static SharedPreferencesHelper getInstance(Context context) {
        return new SharedPreferencesHelper(context, name_file, Context.MODE_PRIVATE);
    }
 
    /**
     * 获取共享引用实例
     *
     * @param context activity的context
     * @param name    文件名称
     * @param mode    权限标识
     * @return
     */
    public static SharedPreferencesHelper getInstance(Context context, String name, int mode) {
        return new SharedPreferencesHelper(context, name, mode);
    }
 
    /**
     * 添加字段
     *
     * @param key   字段名称
     * @param value 字段值
     */
    public void put(String key, Object value) {
        try {
            if (key == null)
                return;
            Editor editor = this.sharedPreferences.edit();// 获取编辑器
            if (value == null) {
                value = "";
            }
            if (value != null) {
                String type = value.getClass().getSimpleName();
                if ("String".equals(type)) {
                    String str = (String) value;
                    // 加密
                    str = Base64.encodeToString(str.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP);
                    editor.putString(key, str);
                } else if ("Integer".equals(type)) {
                    editor.putInt(key, (Integer) value);
                } else if ("Boolean".equals(type)) {
                    editor.putBoolean(key, (Boolean) value);
                } else if ("Float".equals(type)) {
                    editor.putFloat(key, (Float) value);
                } else if ("Long".equals(type)) {
                    editor.putLong(key, (Long) value);
                } else {
                    String str = MyJsonParser.bean2Json(value);
                    str = Base64.encodeToString(str.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP);
                    editor.putString(key, str);
                }
            }
            editor.commit();// 提交修改
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 保存数据的方法,
     *
     * @param headers
     */
    public void put(Map<String, String> headers) {
        try {
            Editor editor = this.sharedPreferences.edit();// 获取编辑器
            if (headers != null) {
                Set<String> keys = headers.keySet();
                for (String key : keys) {
                    Object value = headers.get(key);
                    put(key, value);
                }
            }
            editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 根据字段名称获取字段值
     *
     * @param <T>
     * @param key 字段名称
     *            默认值
     * @return
     */
    public <T> T get(String key, T defaultValue) {
        if (defaultValue == null) {
            return (T) get(key, String.class, defaultValue);
        } else {
            return (T) get(key, defaultValue.getClass(), defaultValue);
        }
    }
 
    /**
     * 根据字段名称获取字段值
     *
     * @param key          字段名称
     * @param valueType    字段值类型
     * @param defaultValue 默认字段值
     * @return 字段值 如果返回值类型不在{String,Integer,Boolean,Float,Long}中,默认返回null.
     */
    private <T> Object get(String key, Class<?> valueType, T defaultValue) {
        String type = valueType.getSimpleName();
        if ("String".equals(type)) {
            String value = this.sharedPreferences.getString(key, "");
            if (TextUtils.isEmpty(value)) {
                return defaultValue;
            } else {
                // 解密
                byte[] bt = Base64.decode(value, Base64.URL_SAFE | Base64.NO_WRAP);
                String strvalue = new String(bt);
                return strvalue;
            }
 
        } else if ("Integer".equals(type)) {
            return this.sharedPreferences.getInt(key, (Integer) defaultValue);
        } else if ("Boolean".equals(type)) {
            return sharedPreferences.getBoolean(key, (Boolean) defaultValue);
        } else if ("Float".equals(type)) {
            return sharedPreferences.getFloat(key, (Float) defaultValue);
        } else if ("Long".equals(type)) {
            return sharedPreferences.getLong(key, (Long) defaultValue);
        }
        return null;
    }
 
    /**
     * 根据字段名称删除字段
     *
     * @param key 字段名称
     */
    public void delete(String key) {
        Editor editor = this.sharedPreferences.edit();
        editor.remove(key);
        editor.commit();
    }
 
    /**
     * 清空所有字段名称
     *
     *
     */
    public void deleteAll() throws Exception {
        Editor editor = this.sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
 
}