4

我有一个线程运行多个操作一次,我想在用户取消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++;
    }
}

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

4

2 回答 2

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();
于 2013-03-12T11:38:48.820 回答
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);
}
于 2013-03-12T11:47:04.043 回答