Android SharedReference值在应用程序关闭时被清除
我正在Android SharedReference值在应用程序关闭时被清除,android,sharedpreferences,android-storage,Android,Sharedpreferences,Android Storage,我正在SharedReference上保存我的UserId,以备将来使用。但如果我关闭或关闭应用程序,数据就会被清除 用于保存的代码: SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = app_preferences.edit(); editor.
SharedReference
上保存我的UserId
,以备将来使用。但如果我关闭或关闭应用程序,数据就会被清除
用于保存的代码:
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = app_preferences.edit();
editor.clear();
editor.putString("USERID", valu1);
editor.apply();
要检索的代码:
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
userID = app_preferences.getString("USERID", "");
有什么建议吗?apply()方法是异步的,在后台线程上工作。它将数据缓存在RAM中并等待,直到有足够的资源将数据写入永久存储器。考虑到这一点,如果你立即关闭你的应用程序,你可能会丢失数据。与apply()不同,commit在UI线程上运行,并同步执行。它有一个保证写入,但会暂停UI线程一段时间。不过,等待并不重要。考虑使用Actuple()代替Apple(),看看它是否有用。
为一个会话管理器创建一个会话分隔符。
public class SessionManager {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "Android_Session";
private static final String IS_LOGIN = "IsLoggedIn";
// User name, Email,Id (make variable public to access from outside)
public static final String KEY_PHONE = "phone";
public static final String KEY_EMAIL = "email";
public static final String KEY_ID = "id";
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session when user login first time call this.
*/
public void createLoginSession(String phone, String email, String id) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_PHONE, phone);
editor.putString(KEY_EMAIL, email);
editor.putString(KEY_ID, id);
editor.commit();
}
/**
* Check user is login or not if not.
* This will used in dynamic app where session expired from server sideit will redirect to login page.
*/
public void checkLogin() {
if (!this.isLoggedIn()) {
// user is not logged in then redirect to Login Activity
Intent i = new Intent(_context, Login.class);
// Add flags launch modes
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_PHONE, pref.getString(KEY_PHONE, null));
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
user.put(KEY_ID, pref.getString(KEY_ID, null));
return user;
}
/**
* Clear session details
*/
public void logoutUser() {
editor.clear();
editor.commit();
// After logout redirect user to Login Activity
Intent i = new Intent(_context, Login.class);
// Add Flags (Launch Modes)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
_context.startActivity(i);
}
/**
* Check if user login or not
**/
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
如果你不关闭应用程序,你能得到值吗?分享整个班级code@ZankrutParmar是的,它是可用的。通过声明自定义SharedReferences修复了该问题。在保存另一个数据之前调用clear()函数。这也导致我删除了这个。这可能对任何人都有帮助。SharedReferences app_preferences=getApplicationContext().GetSharedReferences(“用户数据”,0);试过了,但没用
SessionManager session = new SessionManager(getApplicationContext());
HashMap<String, String> user = session.getUserDetails();
String id = user.get(SessionManager.KEY_ID);
SessionManager session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
//ToDo
} else {
}