首先,用戶登錄您必須聲明全局共享偏好列:
SharedPreferences sp;
然後在你的onCreate SP delcare:
sp = getSharedPreferences("Session", 0); //"Session" is the file name it MUST be the same in all activities that require shared preferences.
所以基本上,當用戶登錄你檢查他是否是登錄後,如果這是真的,然後用保存他的ID在共享偏好:
SharedPreferences.Editor e = sp.edit();
e.putInt("ID", ID-Value-that-you-used-it-to-check-for-ID);
e.commit();
,並在的onCreate登錄類,添加第二塊以檢查用戶是否登錄,他從來沒有登出。如果他隨後帶他去下一個活動。
if (cID != 0)
{
Intent iLogged = new Intent(this, The-Next-Class.class);
finish();
startActivity(iLogged);
}
只要你想找回你只需要添加值
...
int ID = sp.getInt("ID", 0);
,並註銷用戶,然後刪除該會話使用下列內容:
sp.edit().clear().commit();
Intent iLogout = new Intent(this, Login.class);
finish();
startActivity(iLogout);
break;
編輯2 :
你要做的是...
1)從共享首選項中檢索ID ..現在如果UserID列是int,則使用。
int ID = sp.getInt("ID", 0); // 0 is default value which means if couldn't find the value in the shared preferences file give it a value of 0.
2)你必須檢查,如果ID!= 0,則值傳遞給查詢......
SELECT user_id, username, password
FROM users
WHERE user_id = ID
這個用戶ID是一個列,我將從SQLite數據庫中調用並在用戶登錄時將其保存爲會話。你知道如何繼續這樣做嗎? – Rager58
檢查編輯的部分希望你有想法,如果不添加代碼來幫助你更多 – Coderji
我該如何聲明用戶的user_id列作爲sql數據庫中的一個int? e.putInt(「ID」,不知道這部分); 請參閱上面的代碼 – Rager58