| | |
| | | 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 键值 |
| | | * @param context 上下文 |
| | | * @param key 键名 |
| | | * @param value 键值 |
| | | * @return - |
| | | */ |
| | | public static boolean saveMyDataInfo(String key, String value, Context context) { |
| | | public static boolean putString(String key, String value) { |
| | | // 获取SharedPreferences对象,同时指定文件名称和访问权限 |
| | | SharedPreferences sp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE); |
| | | // SharedPreferences sp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE); |
| | | // 获取获取SharedPreferences的编辑器对象 |
| | | SharedPreferences.Editor edit = sp.edit(); |
| | | // 通过编辑器进行数据的存储 |
| | |
| | | /** |
| | | * 读取用户数据 |
| | | * |
| | | * @param key 键名 |
| | | * @param context 上下文 |
| | | * @param key 键名 |
| | | * @return -value |
| | | */ |
| | | public static String getSharedPreferencesKey(String key, Context context) { |
| | | public static String getSharedPreferencesKey(String key) { |
| | | // 获取SharedPreferences对象,同时指定文件名称和访问权限 |
| | | SharedPreferences sp = context.getSharedPreferences("MyData", Context.MODE_PRIVATE); |
| | | 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); |
| | | } |
| | | } |