2012-12-12 2 views
1

나는 몇 가지 컨트롤을 가지고 있는데, 어떤 이유로 스레드를 만들었습니다. asycntask를 사용하여로드 중 한 화면을 다른 화면에 표시하고 싶습니다. 내 문제는로드 표시 줄이 빠르게 표시되고로드가없는 동일한 화면을 기다리는 중입니다. 그리고 잠시 후 두 번째 화면으로 이동합니다. 사후 집행이 제대로 작동하지 않는 것 같아. 아무도 도와 줄 수 있니?onPostExecute()가 너무 오래 기다리고 있습니까?

private class ProgressBarAsync extends AsyncTask<Void, Integer, Void>{ 

     /** This callback method is invoked, before starting the background process */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      /** Creating a progress dialog window */ 
      mProgressDialog = new ProgressDialog(Login.this); 

      /** Close the dialog window on pressing back button */ 
      mProgressDialog.setCancelable(true); 

      /** Setting a horizontal style progress bar */ 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 

      /** Setting a message for this progress dialog 
      * Use the method setTitle(), for setting a title 
      * for the dialog window 
      * */ 
      mProgressDialog = ProgressDialog.show(Login.this, 
        "Giriş yapıyorsunuz", "Lütfen bekleyin..."); 



     } 

     /** This callback method is invoked on calling execute() method 
     * on an instance of this class */ 
     @Override 
     protected Void doInBackground(Void...params) { 

      try { 
       startMyApplication() ; 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** This callback method is invoked when publishProgress() 
     * method is called */ 
     @Override 
     protected void onProgressUpdate(Integer... values) { 
      super.onProgressUpdate(values); 
      mProgressDialog.setProgress(mProgressStatus); 
     } 

     /** This callback method is invoked when the background function 
     * doInBackground() is executed completely */ 
     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      if (mid > 0) { 
       Intent btn_login = new Intent(
         getApplicationContext(), MainScreen.class); 
       startActivity(btn_login); 
      } 
      else { 
       AlertDialog.Builder alertDialog = new AlertDialog.Builder(Login.this); 

       // Setting Dialog Title 
       alertDialog.setTitle("GİRİŞ"); 

       // Setting Dialog Message 
       alertDialog.setMessage("Kullanıcı adı veya Parola Hatalı Lütfen tekrar deneyin."); 

       // Setting Icon to Dialog 
       //alertDialog.setIcon(R.drawable.delete); 

       // Setting Negative "NO" Button 
       alertDialog.setNegativeButton("TAMAM", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 

         dialog.cancel(); 
        } 
       }); 

       // Showing Alert Message 
       alertDialog.show();   
      } 

      //run();      
      mProgressDialog.dismiss(); 

이것은 startmyapp theads :이 섹션은 지연의 원인이되는

public void startMyApplication() throws InterruptedException { 

    ExecutorService executor = Executors.newFixedThreadPool(2); 
    FutureTask<String> futureOne = new FutureTask<String>(
      new Callable<String>() { 
       public String call() throws Exception { 
        if (isOnline()) { 
         // Call Login Web Service with username and password and get mid 
         mid = callLoginWS(username.getText().toString(), password.getText().toString());        
         if (mid > 0) { 
          //callPasswordGeneratorWS(mid); 
          // Insert mid and username into sqllite 
          dbInstance.insertMerch(mid,username.getText().toString()); 
         } 
         Log.i("futureone", "futureone"); 

        } 
        return "TEST"; 
       } 
      }); 

    FutureTask<String> futureTwo = new FutureTask<String>(
      new Callable<String>() { 
       public String call() throws Exception { 
        // Get mid from database 
        mid = dbInstance.selectMerch(username.getText().toString()); 
        return "TEST"; 
       } 

      }); 



    // ... Dispatch 
    // Check if user exists previously 
    // ... Dispatch  
    mid = dbInstance.selectMerch(username.getText().toString()); 
    dbInstance.close(); 


    executor.execute(futureOne); 
    while (!(futureOne.isDone())) { 
     executor.execute(futureTwo); 
    } 
    Log.i("login","new login"); 
    //} 
    executor.shutdown(); 
} 
+0

이 함수는 무엇입니까'startMyApplication(); –

+0

내 질문을 편집했습니다. – DuyguK

답변

3

이 내 코드입니다.

while (!(futureOne.isDone())) { 
     executor.execute(futureTwo); 
    } 

여기 현재 완료 할 수있는 스레드를 차단하고 있습니다. futureOne이 완료 될 때까지 기다리는 중입니다.

0

우선 ExecutorService없이 startMyApplication 메서드를 실행 해보십시오. 에서 코드를 실행할 때 doInBackground은 이미 다른 스레드에 있습니다. (내가 잘못하면 나를 수정하십시오.) 여분의 스레드를 사용하지 않고 코드를 실행하십시오. 이것을 수행하고 모든 것이 작동하는지 확인하십시오.

또한, 나는 당신이 publishProgress (진행 ... 값) doInBackground 방법 어디서나 메소드를 호출하지 않는 것으로 나타났습니다. 따라서 진행률 표시 줄에 올바른 값을 게시 할 수 없습니다.

0

내가 잘못하지 않았다면. AsyncTask를 사용하여 startMyApplication()을 비동기 적으로 실행하고 있으므로 FutureTask를 만드는 이유는 무엇입니까? 이미 백그라운드 작업을하고 있습니다. 이 같은 UR 코드를 수정하려고 doInBackground에서 지금

public Long startMyApplication(){ 
     if (isOnline()) { 
      // Call Login Web Service with username and password and get mid 
      mid = callLoginWS(username.getText().toString(), password.getText().toString());        
      if (mid > 0) { 
       //callPasswordGeneratorWS(mid); 
       // Insert mid and username into sqllite 
       dbInstance.insertMerch(mid,username.getText().toString()); 
      } 
      Log.i("futureone", "futureone"); 

      } 
     mid = dbInstance.selectMerch(username.getText().toString()); //OR directly returning mid without fetching from db as it will be same. 
     return mid; 
    } 

(중간을 고려하면 길이). 이것은 지금까지 내가 그것을 이해로 당신을 위해 작동 할 수

@Override 
protected Long doInBackground(Void...params) { 
      Long temp = -1l; 
      try { 
       temp = startMyApplication() ; 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return temp; 
} 

@Override 
protected void onPostExecute(Long mid) { 
mProgressDialog.dismiss(); //First dismiss the progress bar. 
//Rest of ur code will go same in this. Remember mid is now accessible correctly. 
} 

변경 다음 줄,

private class ProgressBarAsync extends AsyncTask<Void, Integer, Void> 

private class ProgressBarAsync extends AsyncTask<Void, Integer, Long> 

에 의해.

관련 문제