PHP + MySQL を使用してシステムにログインしたと仮定し、アプリを終了した後、アプリを再度実行します。再度ログインする必要はなく、自動ログインします。問題の解決を手伝ってください。
2 に答える
This is what you should to do for Auto login:
1.Obtain a session id from login user web service.This session id is associated with every web service call you make.
2.Just store the session id in SharedPreferences or other form of persistent storage.When the app is restarted check whether the session id is defined or not.Make sure that on every log out you initialize it to null.If the session id in SharedPreferences is defined log the user in automatically.
3.Use this session id to make any further web service calls you make.
初めてログインすると、以下に示すように、ユーザー名またはユーザー ID を sharedpreference に保存するよりも、respones でユーザー名またはユーザー ID を取得します。
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
prefs.edit().putInt("userid",userid).commit();
ログインに成功すると、ユーザーIDが優先的に保存され、すでにログインしているかどうかを確認できます
int Userid;
prefs = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
Userid= mPrefs.getInt("userid", 0); which will return defalut value 0 if u are not login
チェック後
if(Userid>0){
// Means u are already logged in do here code what u want if u are login
}else{
// Means u are not logged in than go to your login pageview from here
}
これは、onclickイベントよりもボタンクリックでログアウト機能が必要な場合に想定することを行うための簡単な方法です。設定をクリアするログアウト用の次のコードを追加する必要があります
prefs = PreferenceManager.getDefaultSharedPreferences(MyAccountActivity.this);
prefs.edit().clear().commit();