2016-10-30 57 views
0

當我登錄我的應用程序時,我希望我的登錄頁面不會再顯示,如果用戶已經登錄過。因爲每次用戶終止應用程序並再次打開應用程序,登錄頁面將顯示加載消息。我想實現它,因爲如果用戶在沒有數據連接的情況下打開應用程序,用戶仍然會轉到他的頁面。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"); 

回答

0

所以,就我的理解是截至目前爲止,你想自動點擊登錄按鈕時,一些用戶在你的應用已經登錄使用的用戶名和之前存儲的密碼在SharedPreferences。所以如果我正確地假設了這個問題,這裏是你如何實現這個。

我只是重寫你的代碼。請參閱評論以獲得更好的理解。

pref = getSharedPreferences("Login.conf", Context.MODE_PRIVATE); 
boolean hasLoggedIn = pref.getBoolean("hasLoggedIn", false); 

// Move the onClickListener outside the if statement 
// Because, if the user is not already logged in 
// Then you need to have a click action on this button. 
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); 
    } 
}); 

if(hasLoggedIn) { 
    String username = pref.getString("username", ""); 
    String password = pref.getString("password", ""); 

    // Remove this 
    // if (!username.equals("") && (!password.equals(""))) { 
    // postData.put("username", username); 
    // postData.put("password", password); 
    // authenticate(postData); 
    // } 

    // Simply put this in here 
    btnLogin.performClick(); 
} 

並保存偏好的authenticate功能

private void authenticate(...) { 

    // On a successful login 
    editor = pref.edit(); 
    editor.putBoolean("hasLoggedIn", true); 
    editor.commit(); 
} 
+0

當我每次回到我的應用程序時,如果刪除了'if(!username.equals(「」))&&(!password.equals(「」)))',則表示用戶名爲空。 – JosephG

+0

嘗試卸載並重新安裝您的應用程序。因爲,您可能已將空字符串存儲在您的首選項中。並請看到最新的答案。 –

0

登錄成功,我不明白爲什麼要if(hasLoggedIn)線永遠是負數了。你總是把true的值放在hasLoggedIn共享偏好中,然後你就有了一個if語句。