从AsyncTask的更新进度对话框中活动 [英] Updating progress dialog in Activity from AsyncTask

查看:111
本文介绍了从AsyncTask的更新进度对话框中活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我做的的AsyncTask 有些紧张工作所建议的Andr​​oid的教程,并呈现出 ProgressDialog 的我的主要我的活动:

In my app I am doing some intense work in AsyncTask as suggested by Android tutorials and showing a ProgressDialog in my main my activity:

dialog = ProgressDialog.show(MyActivity.this, "title", "text");
new MyTask().execute(request);

在这里再后来在 MyTask的我发布结果反馈给活动:

where then later in MyTask I post results back to activity:

class MyTask extends AsyncTask<Request, Void, Result> {

    @Override protected Result doInBackground(Request... params) {
        // do some intense work here and return result
    }

    @Override protected void onPostExecute(Result res) {
        postResult(res);
    }
}

和结果发布,在主要活动我隐藏对话框:

and on result posting, in main activity I hide the dialog:

protected void postResult( Result res ) {
    dialog.dismiss();
    // do something more here with result...
}

所以,一切工作正常这里,但我想以某种方式更新进度对话框能够向用户展示一些真正的进展,而不是仅仅是假的请等待......的消息。我能以某种方式访问​​从 MyTask.doInBackground 进程对话框,在这里所有的工作都做了什么?

So everything is working fine here, but I would like to somehow to update the progress dialog to able to show the user some real progress instead just of dummy "Please wait..." message. Can I somehow access the progress dialog from MyTask.doInBackground, where all work is done?

据我了解正在运行作为单独的线程,这样我就可以从那里没有对话的主要活动,这就是为什么我用 onPostExecute 的结果推回它。但问题是, onPostExecute 被称为只有当所有的工作已经完成,我想在做一些中间的更新进度对话框。

As I understand it is running as separate Thread, so I cannot "talk" to main activity from there and that is why I use onPostExecute to push the result back to it. But the problem is that onPostExecute is called only when all work is already done and I would like to update progress the dialog in the middle of doing something.

任何提示如何做到这一点?

Any tips how to do this?

推荐答案

AsyncTask的有法 onProgressUpdate(整数...),您可以调用每个迭代,例如或每时间进度是在完成doInBackground()致电 publishProgress()

AsyncTask has method onProgressUpdate(Integer...) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().

参阅文档了解更多详情

这篇关于从AsyncTask的更新进度对话框中活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆