这是工具类, 我需要根据用户的id去判断是不是需要重新登录, 第一次安装的时候, 登录进去,然后在后台杀死 , 然后下次打开是还是要重新登录, 通过log查看 sp中保存的值为空 , 但是第二次登录后之后就是好的, 求大神解答 到底是什么 ? 还有 SharedPreferences这个到底靠不靠谱 我第一次遇到这样的问题 很绝望
public class PreferenceUtils {
private static SharedPreferences preferences;
private static String NAME = "config"; private static SharedPreferences getPreferences(Context context) {
if (preferences == null) {
preferences = context.getSharedPreferences(NAME,
context.MODE_PRIVATE);
}
return preferences;
} public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false); } public static boolean getBoolean(Context context, String key,
boolean defValue) {
return getPreferences(context).getBoolean(key, defValue); } // 设置值,设置中心的值
public static void setBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = getPreferences(context);
Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.apply(); } public static String getString(Context context, String key) {
return getString(context, key, null); } public static String getString(Context context, String key, String defValue) {
return getPreferences(context).getString(key, defValue); } // 设置值,设置中心的值
public static void setString(Context context, String key, String value) {
SharedPreferences preferences = getPreferences(context);
Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply(); } // 设置值,设置中心的值
public static void setInt(Context context, String key, int value) {
SharedPreferences preferences = getPreferences(context);
Editor editor = preferences.edit();
editor.putInt(key, value);
editor.apply(); } public static int getInt(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue); } public static int getInt(Context context, String key) {
return getInt(context, key, -1); } public static void clear(Context context) {
SharedPreferences preferences = getPreferences(context);
Editor editor = preferences.edit();
editor.clear();
editor.apply();
}
}
解决方案 »
- SQLiteOpenHelper构造方法的context参数问题
- 【重新贴图提问】videoview过程中的问题?
- 求教:Android的RSA数字签名源码中公钥结构体
- java源码 实现网页更新
- AsyncTask 的问题,我都搞的快吐血了,谁了解进来看两眼。
- 如果android 修改系统界面 (将system_bar移动到左侧)
- 读取其他程序中的sqlite数据
- 请教一个目录是在何处的?
- 安装了android studio,能启动模拟器但是运行程序却不能检测到模拟器怎么回事?
- 如何根据抓到的数据包确定APP的名称
- AS布局视图不显示,实际运行正常,已尝试多种方法
- APP息屏后 后台gps记录中断的问题
是异步提交如果有强烈的同步需求换成同步提交
commit试试