2013-03-12 37 views
4

我有一個線程運行多次操作一次,我想停止它,當用戶取消ProgressDialog取消ProgressDialog並停止線程

public void run() { 

    //operation 1 

    //operation 2 

    //operation 3 

    //operation 4 
} 

這個線程只運行一次,所以我不能實現一個循環來檢查他的線程是否仍然運行。

這裏是我的ProgressDialog:

//Wait dialog 
m_dlgWaiting = ProgressDialog.show(m_ctxContext, 
            m_ctxContext.getText(R.string.app_name), 
            m_ctxContext.getText(R.string.msg_dlg_analyse_pic), 
            true, //indeterminate 
            true, 
            new OnCancelListener() { 
             @Override 
             public void onCancel(DialogInterface dialog) { 
              m_bRunning = false; 
             } 
            }); 

由於我不知道如何停止線程,這將是正確的,通過一個循環測序線程的操作,看它是否仍然應該是運行,或有沒有更好的辦法 ?

public void run() { 
    int op = 0; 
    while(m_bRunning) { 
     switch(op) { 
      case 0 : 
       //operation 1 
       break; 
      case 1 : 
       //operation 2 
       break; 
      case 2 : 
       //operation 3 
       break; 
      case 3 : 
       //operation 4 
       break; 
     } 
     op++; 
    } 
} 

即使有這樣的解決方案,如果在線程太多的操作,也可能是難以測序的操作。有沒有更好的方法來實現這一目標?

回答

9

使用回調或的AsyncTask

http://developer.android.com/reference/android/os/AsyncTask.html

final AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { 
     private ProgressDialog dialog; 

     @Override 
     protected void onPreExecute() 
     { 
      this.dialog = new ProgressDialog(context); 
      this.dialog.setMessage("Loading..."); 
      this.dialog.setCancelable(true); 
      this.dialog.setOnCancelListener(new DialogInterface.OnCancelListener() 
      { 
       @Override 
       public void onCancel(DialogInterface dialog) 
       { 
        // cancel AsyncTask 
        cancel(false); 
       } 
      }); 

      this.dialog.show(); 

     } 

     @Override 
     protected Void doInBackground(Void... params) 
     { 
      // do your stuff 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) 
     { 
      //called on ui thread 
      if (this.dialog != null) { 
       this.dialog.dismiss(); 
      } 
     } 

     @Override 
     protected void onCancelled() 
     { 
      //called on ui thread 
      if (this.dialog != null) { 
       this.dialog.dismiss(); 
      } 
     } 
}; 
task.execute(); 
+0

謝謝,我不確定是否可以使用AsyncTasks,因爲這些操作是同步的,但是我將用此替換我的線程。順便說一下,如果在任務中進行網絡操作,將AsyncTask作爲我的活動的一員,這是一個很好的實踐嗎? – 2013-03-12 11:44:47

+1

是的,你可以使用它作爲一個成員,但是你必須在執行之前每次初始化它(一個實例可以被執行一次)。 – 2013-03-12 11:47:16

1

可以使用的AsyncTask像下面

FetchRSSFeeds fetchRss = new FetchRSSFeeds() 
fetchRss.execute(); 

private class FetchRSSFeeds extends AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this); 

    /** progress dialog to show user that the backup is processing. */ 
    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage(getResources().getString(
       R.string.Loading_String)); 
     this.dialog.show(); 
    } 

    protected Boolean doInBackground(final String... args) { 
     try { 

      /** 
      * Write your RUN method code here 
      */ 
      if (isCancelled()) { 

      if (dialog.isShowing()) { 
       dialog.dismiss(); 
      } 
      } 

      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

    } 
} 

而當你想取消你的後臺進程做如下

if (fetchRss.getStatus() == AsyncTask.Status.RUNNING) { 
    fetchRss.cancel(true); 
}