0

我有以下AsyncTask

public class LoadFromCloudTask extends AsyncTask<Void, String, Boolean> {

    public LoadFromCloudTask(LoadFromCloudTaskFragment loadFromCloudTaskFragment) {
        this.loadFromCloudTaskFragment = loadFromCloudTaskFragment;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        ...
        ...
        ...
        // How possible I can halt my execution, prompt for user input, and continue execution based on user input.
        // In Swing, I can ...
        //
        /*
           // Hold the execution, and prompt for user input
           final int result = JOptionPane.showConfirmDialog(LoadFromCloudJDialog.this, message, MessagesBundle.getString("question_title_overwrite_your_data_by_cloud_data_at"), JOptionPane.YES_NO_OPTION);
           // Continue execution based on user input.
         */
    }
}

我想知道,是否可以停止 AsyncTask 执行,提示用户输入并根据用户输入继续执行?

在 Swing 中,我可以通过简单地使用JOptionPane.showConfirmDialog. 安卓呢?

4

4 回答 4

1

由于doInBackground在不同的线程中执行,您必须在 UI 线程中显示一些对话框时暂停该线程,一旦对话框关闭,您可以恢复AsyncTask并读取结果。

于 2013-03-16T08:21:54.490 回答
1

检查Asynctask的文档,您可以取消并重新启动异步任务,因为The task can be executed only once

你可能想要这样的理想方式来取消执行异步任务吗

doInBackground 不在 UI 线程中执行,因此您可以执行显示对话框的 UI 操作

于 2013-03-16T08:23:53.790 回答
1

您可以通过使用从内部向UIThread发送消息并使用 java 的等待/通知机制来等待用户输入他的输入来实现这一点。publishProgressdoInBackground(Params...)

伪代码看起来像这样:

 public Object mLock = new Object();
 protected void doInBackground(Params... params){
  /// do your requests.
  publishProgress(Progress ...progress);
  mLock.wait();

 }
 protected void onProgressUpdate(Progress ....progress) {
    // request user input here
 }

并从您的 UIThread 调用mLock.notify()以要求代码继续。

此外,您可以通过将逻辑拆分到多个 AsynchTasks 来执行更灵活的行为,因为 AsynchTasks 由共享(静态)ThreadPoolExecutor 和

于 2013-03-16T08:54:08.883 回答
1
You can create pause and resume method inside asyntask as below, and call it according to your convenience.

        public class LoadFromCloudTask extends AsyncTask<Void, String, Boolean> {

            public LoadFromCloudTask(LoadFromCloudTaskFragment loadFromCloudTaskFragment) {
                this.loadFromCloudTaskFragment = loadFromCloudTaskFragment;
            }

    //call this when you want to halt the task        
                    public void pause() {
                           if (this.getStatus().equals(Status.RUNNING)) {
                                   try {
                                           this.wait();
                                   } catch (InterruptedException e) {
                                           e.printStackTrace();
                                   }
                           }
                   }

    // call this when you need to resume the task
                   public void resume() {
                           if (this.getStatus().equals(Status.PENDING)) {
                                   this.notify();
                           }
                   }

            @Override
            protected Boolean doInBackground(Void... params) {
                ...
                ...
                ...
                // How possible I can halt my execution, prompt for user input, and continue execution based on user input.
                // In Swing, I can ...
                //
                /*
                   // Hold the execution, and prompt for user input
                   final int result = JOptionPane.showConfirmDialog(LoadFromCloudJDialog.this, message, MessagesBundle.getString("question_title_overwrite_your_data_by_cloud_data_at"), JOptionPane.YES_NO_OPTION);
                   // Continue execution based on user input.
                 */
            }
        }
于 2013-03-16T10:32:59.803 回答