2015-09-07 31 views
5

我想在登錄成功後開始一項新活動。那就是當登錄進行正確時,一個新的活動應該自動啓動......我不知道在哪裏提到startactivity。請幫助我,這是一個新手android。 這是我的代碼...登錄成功後如何開始新的活動?

public class BackgroundTask extends AsyncTask<String,Void,String> { 
    AlertDialog alertDialog; 
    Context ctx; 
    BackgroundTask(Context ctx) 
    { 
     this.ctx =ctx; 
    } 
    @Override 
    protected void onPreExecute() { 
     alertDialog = new AlertDialog.Builder(ctx).create(); 
     alertDialog.setTitle("Login Information...."); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String reg_url = ""; 
     String login_url = ""; 
     String method = params[0]; 
     if (method.equals("register")) { 
      String name = params[1]; 
      String user_name = params[2]; 
      String user_pass = params[3]; 
      try { 
       URL url = new URL(reg_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 

       //httpURLConnection.setDoInput(true); 
       OutputStream OS = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
       String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + 
         URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
         URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       OS.close(); 
       InputStream IS = httpURLConnection.getInputStream(); 
       IS.close(); 
       //httpURLConnection.connect(); 
       httpURLConnection.disconnect(); 
       return "Registration Success..."; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     else if(method.equals("login")) 
     { 
      String login_name = params[1]; 
      String login_pass = params[2]; 
      try { 
       URL url = new URL(login_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
       String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+ 
         URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 

       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 
       String response = ""; 
       String line = ""; 
       while ((line = bufferedReader.readLine())!=null) 
       { 
        response+= line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return response; 


      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 


     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if(result.equals("Registration Success...")) 
     { 
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      alertDialog.setMessage(result); 
      alertDialog.show(); 
     } 

    } 
} 
+2

對不請自來的建議:如果doInBackground有兩個以上的可能結果,我建議改變onPostExecute以獲得一個Integer參數,這樣你就可以對結果值做一個開關事件:例如「Unknown username,connection timeout 「等 – user5292387

+1

此外,在您發佈的代碼中放置實時網址或其他潛在敏感或專有信息不是一個好主意。可能想清理這些URL。 – user5292387

回答

2

你可以把意圖在onPostExecute方法是這樣的:

@Override 
    protected void onPostExecute(String result) { 
     if(result == null) 
     { 
      // do what you want to do 
     } 
     else if(result.equals("Registration Success...")) 
     { 
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
     } 
     else if(result.contains("Login Success")) // msg you get from success like "Login Success" 
     { 
      Intent i = new Intent(ctx,NewActivity.class); 
      ctx.startActivity(i); 
      alertDialog.setMessage(result); 
      alertDialog.show(); 
     } 

    } 

編輯: 如果你想調用登錄​​成功後新的活動,然後把意圖在其他部分。

+0

感謝您的回覆。但如果我把意圖放在這裏,那麼它會在註冊成功後執行......對嗎? 但我想登錄成功後 –

+0

@rijoraju檢查我編輯的答案。 – SANAT

+0

@rijoraju它可以幫助你嗎? – SANAT

0

在postExecute()方法中,您必須檢查方法變量,請檢查以下代碼。

public class BackgroundTask extends AsyncTask<String,Void,String> { 
AlertDialog alertDialog; 
Context ctx; 
String method; 
BackgroundTask(Context ctx) 
{ 
    this.ctx =ctx; 
} 
@Override 
protected void onPreExecute() { 
    alertDialog = new AlertDialog.Builder(ctx).create(); 
    alertDialog.setTitle("Login Information...."); 
} 

@Override 
protected String doInBackground(String... params) { 
    String reg_url = "http://thecapitalcitychurch.16mb.com/new/register.php"; 
    String login_url = "http://thecapitalcitychurch.16mb.com/new/login.php"; 
    method = params[0]; 
    if (method.equals("register")) { 
     String name = params[1]; 
     String user_name = params[2]; 
     String user_pass = params[3]; 
     try { 
      URL url = new URL(reg_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 

      //httpURLConnection.setDoInput(true); 
      OutputStream OS = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
      String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" + 
        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 
      bufferedWriter.write(data); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      OS.close(); 
      InputStream IS = httpURLConnection.getInputStream(); 
      IS.close(); 
      //httpURLConnection.connect(); 
      httpURLConnection.disconnect(); 
      return "Registration Success..."; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    else if(method.equals("login")) 
    { 
     String login_name = params[1]; 
     String login_pass = params[2]; 
     try { 
      URL url = new URL(login_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 
      httpURLConnection.setDoInput(true); 
      OutputStream outputStream = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
      String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+ 
        URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8"); 
      bufferedWriter.write(data); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      outputStream.close(); 

      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 
      String response = ""; 
      String line = ""; 
      while ((line = bufferedReader.readLine())!=null) 
      { 
       response+= line; 
      } 
      bufferedReader.close(); 
      inputStream.close(); 
      httpURLConnection.disconnect(); 
      return response; 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 
    return null; 
} 

@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
} 

@Override 
protected void onPostExecute(String result) { 
    if(result.equals("Registration Success...")) 
    { 
     Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
    } 
    else 
    { 
     alertDialog.setMessage(result); 
     alertDialog.show(); 
    } 

    if(method.equals("login") && result.equals("Login Success...")) 
    { 
     Intent intent=new Intent(ctx, Dashboard.class); 
     ctx.startActivity(intent); 
    } 
} 
} 
+0

它在method.equals中給出錯誤 –

+0

您必須在BackgroundTask中聲明方法變量global類。 –

+0

對不起,哥們我仍然收到錯誤 你能否只是聲明它請求其請求 –

0

呼叫您的onPostexecute

protected void onPostExecute(String response) { 
     try {Intent intent = new Intent(context, MainActivity.class); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(intent); 
}catch(Exception e){}} 
1

試試這個

Intent intent = new Intent(MainActivity.this, Second.class); 
startActivity(intent); 
0

至於要立即開始另一個活動登錄呼叫被成功那麼你可以開始其他活動無論是從呼叫返回登錄響應或從AsyncTask的onpostExecute方法。

爲了在API流程中實現回調,您只需要創建一個沒有主體(僅聲明)的方法很少的接口,並且這些方法必須在實現該接口的類中實現。將此接口對象作爲asyncTask的構造函數傳遞。一旦登錄調用成功,您需要調用該接口的方法,並在該方法內部啓動另一個活動。