[英]Android - Maintain session after login without getting logout
我正在創建一個名稱為“ MainActivity.class”的活動,在該活動中我正在登錄,而在下一個活動中,我正在注銷。
記錄后,我正在使用sessionmanager.class維護會話,其中正在使用checklogin()方法。
在mainactivity中,由於哪個應用由於堆問題而無法啟動,因此我正在使用session.checklogin()方法。 如果我不使用上述方法,則該會話將無法維護。
請協助我維持會議。
這是sessionmanager類中checklogin()的代碼-
public void checkLogin() {
// Check login status
if (this.isLoggedIn()) {
// user is logged in redirect him to Login Activity
Intent i = new Intent(_context, Home.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
} else
if (!this.isLoggedIn()) {
// user is not logged in redirect him to Login Activity
Intent i = new Intent(_context, FirstActivity.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
在MainActivity.class中,我正在使用-
SessionManager會話;
在onCreateMethod()中,我正在執行-session = new SessionManager(getApplicationContext()); 然后傳遞session.checklogin();
需要清除三點。
1.Session可以通過使用Android中的Shared Preferences來管理。 它可以用於將數據存儲為鍵值對。
2.一旦您的應用程序完成服務器通信,就不會有任何會話使它保持活動狀態。
3.SessionManager類實際上並不用於維護會話。 它作為隊列管理媒體會話。
請參閱以下文檔。 這將是有幫助的。
共享首選項
http://developer.android.com/reference/android/content/SharedPreferences.html
SessionManaget http://developer.android.com/samples/MediaRouter/src/com.example.android.mediarouter/player/SessionManager.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.