wjc
2023-06-14 b171f6d0354b418ed5f56fdf5e0d9d9b32fb4254
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
package com.hdl.photovoltaic.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
 
 
public class SharedPreUtils {
 
 
    /**
     * 用户数据的存储
     *
     * @param key     键名
     * @param value   键值
     * @param context 上下文
     * @return -
     */
    public static boolean saveMyDataInfo(String key, String value, Context context) {
        // 获取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     键名
     * @param context 上下文
     * @return -value
     */
    public static String getSharedPreferencesKey(String key, Context context) {
        // 获取SharedPreferences对象,同时指定文件名称和访问权限
        SharedPreferences sp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }
}