在用户成功登录后,使用SharedPreferences将登录状态和用户信息(如手机号、Token等)保存在本地存储中。
目录
1.保存登录状态
在用户成功登录后,将登录状态和用户信息(如手机号、Token等)保存在本地存储中。常用的方法有:
- SharedPreferences:适合保存简单的键值对信息,比如用户ID、Token等。
- SQLite数据库:适合保存更复杂的数据结构。
这里以SharedPreferences
为例:
SharedPreferences sharedPreferences = getSharedPreferences("UserPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("phone_number", phoneNumber); // 保存手机号
editor.putString("auth_token", authToken); // 保存认证Token(如果有)
editor.putBoolean("is_logged_in", true); // 保存登录状态
editor.apply();
2.检查登录状态
在应用启动时,检查用户是否已经登录过。如果是,则跳过登录页面,直接进入主页面。
SharedPreferences sharedPreferences = getSharedPreferences("UserPrefs", MODE_PRIVATE);
boolean isLoggedIn = sharedPreferences.getBoolean("is_logged_in", false);
if (isLoggedIn) {
// 用户已登录,跳过登录页面
String phoneNumber = sharedPreferences.getString("phone_number", null);
String authToken = sharedPreferences.getString("auth_token", null);
// 使用手机号和Token进行必要的验证或初始化
// 跳转到主页面
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 用户未登录,跳转到登录页面
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
3.注销登录
如果用户选择注销登录,则需要清除本地存储中的登录信息,并将is_logged_in
标志设置为false
。
SharedPreferences sharedPreferences = getSharedPreferences("UserPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); // 清除所有保存的信息
editor.apply();
// 返回到登录页面
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
4.安全性考虑
- Token:如果使用Token认证,建议定期刷新Token,并设置过期时间。
- 敏感信息保护:尽量避免明文存储敏感信息,考虑使用加密手段来保护用户数据
5.封装SharedPreferences工具类以便使用
新建java类PreferencesUtil
public class PreferencesUtil {
public static String PREFERENCE_NAME = "user";
/**用户名的key值*/
public static String USERNAME = "username";
/**存储字符串*/
public static boolean putString(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
/**读取字符串*/
public static String getString(Context context, String key) {
return getString(context, key, null);
}
/**读取字符串(带默认值的)*/
public static String getString(Context context, String key, String defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getString(key, defaultValue);
}
/**存储整型数字*/
public static boolean putInt(Context context, String key, int value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
return editor.commit();
}
/**读取整型数字*/
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
/**读取整型数字(带默认值的)*/
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getInt(key, defaultValue);
}
/**存储长整型数字*/
public static boolean putLong(Context context, String key, long value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong(key, value);
return editor.commit();
}
/**读取长整型数字*/
public static long getLong(Context context, String key) {
return getLong(context, key, 0xffffffff);
}
/**读取长整型数字(带默认值的)*/
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getLong(key, defaultValue);
}
/**存储Float数字*/
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**读取Float数字*/
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1.0f);
}
/**读取Float数字(带默认值的)*/
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getFloat(key, defaultValue);
}
/**存储boolean类型数据*/
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
/**读取boolean类型数据*/
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
/**读取boolean类型数据(带默认值的)*/
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, 0);
return preferences.getBoolean(key, defaultValue);
}
/**清除数据*/
public static boolean clearPreferences(Context context) {
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, 0);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
return editor.commit();
}
}