當我登錄我的應用程序時,我希望我的登錄頁面不會再顯示,如果用戶已經登錄過。因爲每次用戶終止應用程序並再次打開應用程序,登錄頁面將顯示加載消息。我想實現它,因爲如果用戶在沒有數據連接的情況下打開應用程序,用戶仍然會轉到他的頁面。Android Studio:共享首選項登錄頁面仍顯示並加載SharedPreference
那麼這裏是我的代碼。
pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
editor = pref.edit();
editor.putBoolean("hasLoggedIn", true);
editor.commit();
boolean hasLoggedIn = pref.getBoolean("hasLoggedIn", false);
if(hasLoggedIn){
String username = pref.getString("username", "");
String password = pref.getString("password", "");
if (!username.equals("") && (!password.equals(""))) {
postData.put("username", username);
postData.put("password", password);
authenticate(postData);
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (TextUtils.isEmpty(etUsername.getText().toString())) {
Toast.makeText(LoginActivity.this, "Username is empty", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(etPassword.getText().toString())) {
Toast.makeText(LoginActivity.this, "Password is empty", Toast.LENGTH_SHORT).show();
return;
}
editor = pref.edit();
postData.put("username", etUsername.getText().toString());
postData.put("password", MD5.encrypt(etPassword.getText().toString()));
editor.commit();
authenticate(postData);
}
});
}
身份驗證
private void authenticate(final HashMap<String, String> postData){
PostResponseAsyncTask task1 = new PostResponseAsyncTask(LoginActivity.this, postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(TAG, s);
if (s.contains("renter")) {
// Login success, Save to prefs
editor = pref.edit();
editor.clear();
editor.putString("username", postData.get("username"));
editor.putString("password", postData.get("password"));
editor.putString("userlevel", s);
editor.commit();
Toast.makeText(LoginActivity.this, "Renter Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(LoginActivity.this, RenterTabs.class);
startActivity(in);
finish();
} else if (s.contains("owner")) {
// Login success, Save to prefs
editor = pref.edit();
editor.clear();
editor.putString("username", postData.get("username"));
editor.putString("password", postData.get("password"));
editor.putString("userlevel", s);
editor.commit();
Toast.makeText(LoginActivity.this, "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(LoginActivity.this, OwnerTabs.class);
startActivity(in);
finish();
} else if (s.contains("driver")) {
editor = pref.edit();
editor.clear();
editor.putString("username", postData.get("username"));
editor.putString("password", postData.get("password"));
editor.putString("userlevel", s);
editor.commit();
Toast.makeText(LoginActivity.this, "Driver Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(LoginActivity.this, DriverTabs.class);
startActivity(in);
finish();
}else if(s.contains("-1")){
Toast.makeText(LoginActivity.this, "Wrong username or password...", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/carkila/authenticate.php");
當我每次回到我的應用程序時,如果刪除了'if(!username.equals(「」))&&(!password.equals(「」)))',則表示用戶名爲空。 – JosephG
嘗試卸載並重新安裝您的應用程序。因爲,您可能已將空字符串存儲在您的首選項中。並請看到最新的答案。 –