2013-12-09 36 views
2

我想讓一個用戶不需要再次登錄,一次登錄後。我的代碼不會跳過登錄頁面。如何跳過登錄頁面?我有一個開始頁面。我按開始按鈕,它進入登錄頁面。但是用戶已經登錄,使得跳過登錄頁面並轉到下一頁。 我該怎麼辦?如何讓用戶不需要再次登錄android

JSONObject json=null; 
         int status=2; 
         String userID=null; 
         String teamID=null; 
         String teamName=null; 
         String admin=null; 
         String backNo=null; 
         String userName=null; 

         ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
         postParameters.add(new BasicNameValuePair("email",edit_txt_EmailAddress.getText().toString()));//// define the parameter 
         postParameters.add(new BasicNameValuePair("password",edit_txt_Password.getText().toString())); 

         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
          .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread 
          .penaltyLog().build()); 

         ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>(); 
         //postParameters.add(new BasicNameValuePair("userID","396797666")); 

         String response = null; 
         try { 
           response=CustomHttpClient.executeHttpPost("http://10.0.2.2/kwikwi/login.php", postParameters); 
           json=new JSONObject(response); 
           userID=json.getString("userID"); 
          } 
         catch (Exception e) { 

          e.printStackTrace(); 
         } 
         Log.i("USERID", userID+""); 
         JSONObject json_data = null; 
         String teamStatus =""; 

         String result = response.toString(); 
         JSONObject jsonobj = null; 
         try{ 

         jsonobj = new JSONObject (result); 
         teamStatus=jsonobj.getString("teamStatus"); 

         } 
         catch(JSONException e){ 
          Log.e("log_tag", "Error parsing data "+e.toString()); 
         } 

         Log.i("TeamStatus", result+""); 

         Log.i("RESULT", teamStatus+""); 
         //Log.i("halo",teamStatus+""); 

         String[] teamSaving=new String[6]; 
         String[] noteamSaving=new String[2]; 

        if (blNagSetting == true){ 
         if(teamStatus.equals("1")){ 
          try { 
           Log.i("Team Kyaw Win tal","Log in ma twar buu."); 

           userName=jsonobj.getString("userName"); 
           userID=jsonobj.getString("userID"); 
           teamID=jsonobj.getString("teamID"); 
           teamName=jsonobj.getString("teamName"); 
           admin=jsonobj.getString("admin"); 
           backNo=jsonobj.getString("backNo"); 

           teamSaving[0]=userName; 
           teamSaving[1]=userID; 
           teamSaving[2]=teamID; 
           teamSaving[3]=teamName; 
           teamSaving[4]=admin; 
           teamSaving[5]=backNo; 

           Log.i("USerName", userName+""); 
           Log.i("UserID",userID+""); 
           Log.i("TeamId",teamID+""); 

          } catch (JSONException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 


          Intent teamshi=new Intent(Login.this,Team.class); 
          teamshi.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(teamshi); 
          finish(); 

         } 
         else if(teamStatus.equals("0")) 
         { 
          try { 

           Log.i("NoTeam Kyaw Win tal","Log in ma twar buu."); 
           userName=jsonobj.getString("userName"); 
           userID=jsonobj.getString("userID"); 

           noteamSaving[0]=userName; 
           noteamSaving[1]=userID; 

           Log.i("USerNameNN", userName+""); 
           Log.i("UserIDNN",userID+""); 

          } catch (JSONException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          Intent noteamshi=new Intent(Login.this,NoTeam.class); 
          noteamshi.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(noteamshi); 
          finish(); 
         } 

        }else 
        { 
         if(teamStatus.equals("1")){ 
          try { 
           Log.i("Team Win tal","Log in hmar"); 

           userName=jsonobj.getString("userName"); 
           userID=jsonobj.getString("userID"); 
           teamID=jsonobj.getString("teamID"); 
           teamName=jsonobj.getString("teamName"); 
           admin=jsonobj.getString("admin"); 
           backNo=jsonobj.getString("backNo"); 

           teamSaving[0]=userName; 
           teamSaving[1]=userID; 
           teamSaving[2]=teamID; 
           teamSaving[3]=teamName; 
           teamSaving[4]=admin; 
           teamSaving[5]=backNo; 

           Log.i("USerName", userName+""); 
           Log.i("UserID",userID+""); 
           Log.i("TeamId",teamID+""); 



          } catch (JSONException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          editor = prefsNagSetting.edit(); 
          editor.putBoolean(NAG_SETTING, true); 
          editor.commit(); 

          Intent teamshi=new Intent(Login.this,Team.class); 
          teamshi.putExtra("TeamSaving", teamSaving); 
          startActivity(teamshi); 


         } 
         else if(teamStatus.equals("0")) 
         { 
          try { 
           Log.i("noTeam Win tal","Log in hmar"); 

           userName=jsonobj.getString("userName"); 
           userID=jsonobj.getString("userID"); 

           noteamSaving[0]=userName; 
           noteamSaving[1]=userID; 

           Log.i("USerNameNN", userName+""); 
           Log.i("UserIDNN",userID+""); 

          } catch (JSONException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 

          editor = prefsNagSetting.edit(); 
          editor.putBoolean(NAG_SETTING, true); 
          editor.commit(); 

          Intent noteamshi=new Intent(Login.this,NoTeam.class); 
          noteamshi.putExtra("NoTeamSaving", noteamSaving); 
          startActivity(noteamshi); 

         } 
         else{ 
          showAlertDialog(Login.this,"Invalid Email and Password", "Enter Valid Email and Password", false); 
         } 
        } 
        } 

這是我的代碼。

public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      isInternetPresent = conDetector.isConnectingToInternet(); 
      Boolean save=false; 

      if (isInternetPresent){ 
        isLoggedIn(); 
        saveLoggedIn(save); 
        if (isLoggedIn()){ 
         Intent team= new Intent(MainActivity.this,Team.class); 
         startActivity(team); 
        } 
        else{ 

        Intent goLogin=new Intent(MainActivity.this,Login.class); 
        startActivity(goLogin); 
        } 
      } 
      else{ 
       showAlertDialog(MainActivity.this, "No Internet Connection", 
          "You don't have internet connection.", false); 
      } 
     } 
    }); 
} 

private boolean isLoggedIn() { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    //The false represents the default value, if the variable is not stored 
    boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false); 
    return isLoggedIn; 
} 

private void saveLoggedIn(boolean value) { 
    SharedPreferences sharedPreferences = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    Editor editor = sharedPreferences.edit(); 
    editor.putBoolean("isLoggedIn", value); 
    editor.commit(); 
} 

這是我的編輯代碼。

回答

0

您可以在您的LAUNCHER ACTIVITY中創建條件語句。讓它像一個啓動畫面或其他東西 這就是說,您可以將用戶名和密碼與isRememeber=true/false一起存儲在您的共享首選項中。並且當用戶啓動應用程序時,啓動畫面活動可以檢查用戶是否已將isRememeber活動設置爲true或false,並且可以啓動MainActivityLoginActivity

幾乎像這裏這個例子

Intent login=new Intent(Splashscreen.this,LoginActivity.class); 
Intent Main=new Intent(Splashscreen.this,MainActivity.class); 
...................... 
//Checking the shared preference here and storing the isRemebered value to a variable 
if(isRemembered) 
startActivity(Main); 
else 
startActivity(login); 

希望這有助於你,你所要做的就是讓有上馬用作啓動活動上啓動畫面的詳細信息的附加活動去here

+0

我已經使用了共享首選項,但我沒有看到是記住你說...編輯器怎麼樣......如果你知道,請告訴我關於這個,plz。 – vcav

3

在你的明星活動,您應該驗證用戶是否已經登錄。

如何知道這個?

您將需要一個布爾變量來知道。一個持久變量。所以,當你創建開始活動,你會放:

if (variable){ 
    goToLoggedActivity(); 
}else{ 
    goToLogInActivity(); 
} 

當用戶登錄時,你需要設置這個變量真正

當用戶註銷時,你需要設置這個變量

如何使它執着?

看看這些功能。

private boolean isLoggedIn() { 
     SharedPreferences sharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     //The false represents the default value, if the variable is not stored 
     boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false); 
     return isLoggedIn; 
    } 

    private void saveLoggedIn(boolean value) { 
     SharedPreferences sharedPreferences = PreferenceManager 
       .getDefaultSharedPreferences(this); 
     Editor editor = sharedPreferences.edit(); 
     editor.putBoolean("isLoggedIn", value); 
     editor.commit(); 
    } 

存儲上面只需調用此函數的變量。

另一種好方法,使之可以看到here

+0

我在哪裏放這些句子......在登錄頁面或開始頁面。 – vcav

+0

這取決於你想要什麼。如果你想在用戶點擊按鈕後進入記錄頁面,你應該把它放在按鈕的頁面中。 –

+0

讓我再解釋一下。我的首頁有一個按鈕。按下時,它將進入登錄頁面。但用戶已經登錄,按開始按鈕,它將跳過登錄頁面。好。你的第一個代碼將在onClick開始按鈕,下一個代碼在登錄...對嗎? – vcav

0

用戶登錄保存SharedPreference後,用戶登錄。

當應用程序開始後,如果用戶已共享的偏好,這意味着你可以跳過該活動並打開登錄後您可以進行的活動。

要考慮的事情 - 如果某人以某種方式僞造了SharedPreference會怎麼樣?請確保它包含的內容稍後會在服務器上進行驗證,以確保它是由真實登錄名生成的,並且不會被僞造(甚至可能實現OAuth協議)。

相關問題