取消ProgressDialog并停止线程

时间:2013-03-12 11:31:52

标签: android multithreading 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++;
    }
}

即使使用此解决方案,如果线程中的操作过多,则可能很难对操作进行排序。有没有更好的方法来实现这一目标?

2 个答案:

答案 0 :(得分:10)

使用回调或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();

答案 1 :(得分: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);
}