2013-03-12 39 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); 
}