wjc
2024-12-23 1863007fd5fc407af48a27c47362e0b1345b668a
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
package com.hdl.photovoltaic.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.hdl.photovoltaic.HDLApp;
 
 
public class SharedPreUtils {
 
    private static final String FILE_NAME = "MyData";
    private static SharedPreferences sp;
 
    public static void init(Context context) {
        sp = context.getApplicationContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    }
 
    /**
     * 用户数据的存储
     *
     * @param key   键名
     * @param value 键值
     * @return -
     */
    public static boolean putString(String key, String value) {
        // 获取SharedPreferences对象,同时指定文件名称和访问权限
//        SharedPreferences sp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE);
        // 获取获取SharedPreferences的编辑器对象
        SharedPreferences.Editor edit = sp.edit();
        // 通过编辑器进行数据的存储
        edit.putString(key, value);
        edit.apply();
        return true;
    }
 
    /**
     * 读取用户数据
     *
     * @param key 键名
     * @return -value
     */
    public static String getSharedPreferencesKey(String key) {
        // 获取SharedPreferences对象,同时指定文件名称和访问权限
        return sp.getString(key, "");
    }
 
 
    /**
     * 添加boolean值
     */
    public static void putBoolean(String key, Boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }
 
    /**
     * 获取boolean值
     */
    public static Boolean getBoolean(String key) {
        // 获取SharedPreferences对象,同时指定文件名称和访问权限
        return sp.getBoolean(key, false);
    }
 
    /**
     * 判断是否存在
     */
    public static Boolean contains(String key) {
        // 获取SharedPreferences对象,同时指定文件名称和访问权限
        return sp.contains(key);
    }
}