【问题标题】:Wait for executed AsyncTask with ProgressDialog使用 ProgressDialog 等待执行的 AsyncTask
【发布时间】:2011-06-17 23:58:53
【问题描述】:

我有一个方法 public void writeEntry(Activity ctx, Entry entry) 获取一些数据并且必须调用本机方法,这需要更长的时间才能完成。
所以我创建了一个 AsyncTask 来处理 ProgressDialog 和本机方法。它在自己的 Activity 中测试它的效果很好,在那个 Activity 中我使用了回调接口等等。

就我而言,我有上述方法并且必须执行 AsyncTask。执行不能在该方法中,因为它不会停止进一步的执行。
在继续执行之前,我需要本机方法的结果。
是否有可能等待 AsyncTask 直到它完成?方法 wait() 不是一个选项,因为 UI 线程也会等待,因此 ProgressDialog 的意义将会丢失。

我可以使用给定参数中的方法runOnUiThread() 还是启动自己的Activity的唯一解决方案?

【问题讨论】:

  • 那里,在哪里?我对活动描述感到困惑。请您写名字而不是使用“那里”,以便我们知道我们正在查看多少不同的活动,谢谢,否则只需在您的问题中转储相关代码:)
  • 我已经编辑了 Q。我希望现在已经清楚了。一般只有主Activity,调用方法writeEntry()
  • 我试图理解它,所以基本上你有执行 AsyncTask 的 wrtieEntry 方法,但是(writeEntry)代码执行仍在继续。但是您希望该代码等到 asynctask 完成它的工作。我说的对吗?
  • 是的,你是对的。我认为 AsyncTask 不是它的正确选择。
  • 嗯,拥有 AsyncTask 的整个概念是保持当前线程运行并在后台执行一些进程,我的建议是在我的回答中;)

标签: android android-asynctask progressdialog intentservice


【解决方案1】:

所以我会尽量解释

在 AsyncTask 中启动繁重的进程,但是在 AsyncTask 完成后要执行的任何代码都将其放在单独的公共方法中。现在,一旦您完成繁重的流程调用,在 onPostExecute() 中单独创建的方法。

所以伪代码看起来像这样,

class main extends Activity {

    class Something extends AsyncTask<String, Integer, String> {

        protected void onPreExecute() {
            // Start your progress bar...
        }

        protected String doInBackground(String... params) {
            // Do your heavy stuff...
            return null;
        }

        protected void onPostExecute(String result) {
            // close your progress dialog and than call method which has
            // code you are wishing to execute after AsyncTask.
        }
    }

}

希望这会有所帮助,

祝你好运!

【讨论】:

  • 如何在不冻结 UI 线程的情况下使用该代码片段等待 AsyncTask 的结果?
【解决方案2】:

我的第一个解决方案是使用带有接口实现的回调方法,参见示例https://stackoverflow.com/a/6396376/390177

在Android聊天中聊了一会儿,听说有一个更实用的解决方案。
您可以将 IntentServicePendingIntent 结合使用。
通信是通过Intent的实现的。
如果你想使用一个ProgressDialog,你需要一个自己的Activity,例如注册一个BroadcastRecieverIntentService 每个广播发送它的实际状态。

但是让我们现在开始吧。
首先我们创建Activity,它包含ProgressDialog和一个注册的BroadcastReceiverBroadcastReceiver 监听有关更新和完成对话的消息。

对于Activity,我们需要布局...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#80000000">
</LinearLayout>

...及相关代码:

public class ProgressActivity extends Activity {
    /**
     * ProgressDialog which is shown
     */
    private ProgressDialog progessDialog_g;

    /**
     * Instance of the BroadcastReceiver
     */
    private BroadcastReceiver receiver_g;

    /**
     * Identifier for the different settings of the ProgressDialog
     */
    public static final String PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR = "pbar_horizontal_bar";
    public static final String PROGRESS_DIALOG_BOOL_CANCELABLE = "pbar_horizontal_cancelable";
    public static final String PROGRESS_DIALOG_STR_MESSAGE = "pbar_message";
    public static final String PROGRESS_DIALOG_INT_MAX = "pbar_max_bar";
    public static final String PROGRESS_DIALOG_INT_VALUE = "pbar_value";
    protected static final int PROGRESS_DIALOG_INT_MAX_VALUE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.progress);

        progessDialog_g = new ProgressDialog(this);

        // Reads and sets the settings for the ProgressDialog
        Intent i = getIntent();
        progessDialog_g.setCancelable(i.getBooleanExtra(
                PROGRESS_DIALOG_BOOL_CANCELABLE, false));
        if (i.getBooleanExtra(
                PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, false)) {
            progessDialog_g.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        } else {
            progessDialog_g.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        progessDialog_g
                .setMessage(i
                        .getStringExtra(PROGRESS_DIALOG_STR_MESSAGE));
        progessDialog_g.setMax(i.getIntExtra(
                PROGRESS_DIALOG_INT_MAX, 100));

        // Create the IntentFilter for the different broadcast messages
        IntentFilter iFilter =
                new IntentFilter(
                        ExampleProgressService.PROGRESS_DIALOG_BROADCAST_INIT);
        iFilter.addAction(ExampleProgressService.PROGRESS_DIALOG_BROADCAST_UPDATE);
        iFilter.addAction(ExampleProgressService.PROGRESS_DIALOG_BROADCAST_FINISH);

        // Creates the BroadcastReceiver
        receiver_g = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Log.d(DefaultPreferences.DEBUG_PREFIX + "ProgressActivity",
                        intent.getAction());

                if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_INIT
                        .equals(intent.getAction())) {
                    // Sets the ProgressDialog style
                    if (intent
                            .getBooleanExtra(
                                    PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR,
                                    false)) {
                        progessDialog_g
                                .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    } else {
                        progessDialog_g
                                .setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    }

                    // Shows the ProgressDialog    
                    progessDialog_g.show();
                } else if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_UPDATE
                        .equals(intent.getAction())) {
                    // Updates the ProgressDialog
                    int value =
                            intent.getIntExtra(
                                    PROGRESS_DIALOG_INT_VALUE,
                                    -1);
                    if (value != -1) {
                        progessDialog_g.setProgress(value);
                    }
                } else if (ExampleProgressService.PROGRESS_DIALOG_BROADCAST_FINISH
                        .equals(intent.getAction())) {
                    // Finishs the ProgressDialog
                    progessDialog_g.cancel();
                    finish();
                }
            }
        };

        // Registers the BroadcastReceiver
        registerReceiver(receiver_g, iFilter);
    }

    @Override
    protected void onDestroy(){
        unregisterReceiver(receiver_g);
        super.onDestroy();
    }
}

现在我们要使用 Activity,让我们开始调用它:

final Intent i = new Intent(parentActivity, <packages>.ProgressActivity);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_BOOL_CANCELABLE, cancelable_g);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR, showProgress_g);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_STR_MESSAGE, message_g);
i.putExtra(ProgressActivity.PROGRESS_DIALOG_INT_MAX, ProgressActivity.PROGRESS_DIALOG_INT_MAX_VALUE);

parentActivity.startActivity(i);

所以我们有一个正在运行的 ProgressActivity,它等待不同的广播。但首先我们需要发送广播的IntentService
那我们走吧:

public class ExampleProgressService extends IntentService {
    /**
     * PendingIntent for callback.
     */
    protected PendingIntent pi_g = null;

    private static final String DEBUG_TAG = "ExampleProgressService";

    /**
     * Message identifier for ProgressDialog init
     */
    public static final String PROGRESS_DIALOG_BROADCAST_INIT = "Dialog.Progress.Init";
    /**
     * Message identifier for ProgressDialog finish
     */
    public static final String PROGRESS_DIALOG_BROADCAST_FINISH = "Dialog.Progress.Finish";
    /**
     * Message identifier for ProgressDialog update
     */
    public static final String PROGRESS_DIALOG_BROADCAST_UPDATE = "Dialog.Progress.Update";

    /**
     * Identifier of the result for intent content
     */
    public static final String PROGRESS_DATA_RESULT = "Result";
    /**
     * Identifier of the result error for intent content
     */
    public static final String PROGRESS_DATA_RESULT_ERROR_MESSAGE = "Result.Error.Message";
    /**
     * Identifier of the result error exception for intent content
     */
    public static final String PROGRESS_DATA_RESULT_ERROR_EXCEPTION = "Result.Error.Exception";
    /**
     * Identifier of the result status for intent content
     */
    public static final String PROGRESS_DATA_RESULT_STATUS_BOOL = "Result.Status.boolean";

    /**
     * Identifier of the pending intent for intent content
     */
    public static final String PROGRESS_DATA_PENDING_RESULT = "PendingResult";

    public ExampleProgressService() {
        super("ExampleProgressService");
    }

    /**
     * Send the finish message.
     */
    private void closeProgressActivity() {
        Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_FINISH);

        sendBroadcast(intent);
    }

    /**
     * Do some magic with the intent content
     */
    private void extractVariablesFromIntentAndPrepare(Intent intent)
            throws Exception {
        pi_g = (PendingIntent) intent
                .getParcelableExtra(PROGRESS_DATA_PENDING_RESULT);

        if (pi_g == null) {
            throw new Exception("There is no pending intent!");
    }

    /**
     * Sends an error message.
     */
    private void failed(Exception e, String message) {
        Intent i = new Intent();
        i.putExtra(PROGRESS_DATA_RESULT_ERROR_EXCEPTION, e);
        i.putExtra(PROGRESS_DATA_RESULT_ERROR_MESSAGE, message);

        send(i, false);
    }

    /**
     * Sends the init message.
     */
    private void initProgressActivity() {
        Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_INIT);

        intent.putExtra(PROGRESS_DIALOG_BOOL_HORIZONTAL_BAR,
                multipart_g);

        sendBroadcast(intent);
    }

    /**
     * (non-Javadoc)
     * 
     * @see android.app.IntentService#onHandleIntent(android.content.Intent)
     */
    @Override
    protected void onHandleIntent(Intent intent) {
        extractVariablesFromIntentAndPrepare(intent);

        initProgressActivity();

        // do your calculation here and implements following code
        Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_UPDATE);

        intent.putExtra(PROGRESS_DIALOG_INT_VALUE, progressValue);

        sendBroadcast(intent);

        // If you finished, use one of the two methods to send the result or an error
        success(result);
        failed(exception, optionalMessage);
    }

    /**
     * Sends the data to the calling Activity
     */
    private void send(Intent resultData, boolean status) {
        resultData.putExtra(PROGRESS_DATA_RESULT_STATUS_BOOL, status);

        closeProgressActivity();

        try {
            pi_g.send(this, Activity.RESULT_OK, resultData);
        } catch (PendingIntent.CanceledException e) {
            Log.e(DEBUG_TAG,
                    "There is something wrong with the pending intent", e);
        }
    }

    /**
     * Sends the result message.
     */
    private void success(String result) {
        Intent i = new Intent();
        i.putExtra(PROGRESS_DATA_RESULT, result);

        send(i, true);
    }
}

计算进度的结果应该在parentActivity中得到,所以我们在那个Activity中创建PendingIntent并调用意图服务

// Some identifier for the call
int requestCode = 12345;

final Intent sI = new Intent(ExampleProgressService.PROGRESS_SERVICE_ACTION);

// Callback
sI.putExtra(ExampleProgressService.PROGRESS_DATA_PENDING_RESULT, parentActivity
        .createPendingResult(requestCode, null,
                PendingIntent.FLAG_CANCEL_CURRENT));

// Service start
parentActivity.startService(sI);

为了接收结果,我们必须重写方法onActivityResult(int requestCode, int resultCode, Intent data)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // Compares the requestCode with the requestCode from above
    if (requestCode == ...) {
        if (data.getBooleanExtra(ExampleProgressService.PROGRESS_DATA_RESULT_STATUS_BOOL, false)) {
            // Calculation was success
            data.getStringExtra(ExampleProgressService.PROGRESS_DATA_RESULT);
        } else
        {
            // Calculation is failed
            data.getStringExtra(ExampleProgressService.PROGRESS_DATA_RESULT_ERROR_MESSAGE);
            ((Exception) data.getSerializableExtra(ExampleProgressService.PROGRESS_DATA_RESULT_ERROR_EXCEPTION));
        }
    }
}

这就是魔法,希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多