从 AsyncTask 更新 Activity 中的进度对话框

发布于 2024-10-09 22:27:13 字数 1053 浏览 4 评论 0原文

在我的应用程序中,我按照 Android 教程的建议在 AsyncTask 中进行了一些紧张的工作,并在我的主要活动中显示了 ProgressDialog

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

然后在 MyTask 中代码> 我将结果发布回活动:

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);
    }
}

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

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

所以这里一切正常,但我想以某种方式更新进度对话框,以便能够向用户显示一些实际进度只是虚拟的“请稍候...”消息。我能否以某种方式从 MyTask.doInBackground 访问进度对话框(所有工作都已完成)?

据我了解,它作为单独的线程运行,因此我无法从那里与主要活动“对话”,这就是为什么我使用 onPostExecute 将结果推回给它。但问题是,只有当所有工作都已完成并且我想在做某事的过程中更新对话框的进度时,才会调用 onPostExecute

有什么技巧可以做到这一点吗?

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);

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...
}

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2024-10-16 22:27:13

AsyncTask 具有方法 onProgressUpdate(Integer...),您可以在每次迭代时调用该方法,或者在每次在 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().

Refer to the docs for more details

情徒 2024-10-16 22:27:13

您可以从 AsyncTask 的方法 onProgressUpdate(YOUR_PROGRESS) 进行更新,该方法可以通过调用 publishProgress(YOUR_PROGRESS)doInBackground 方法调用
YOUR_PROGRESS 的数据类型可以通过 AsyncTask 定义

you can update from AsyncTask's method onProgressUpdate(YOUR_PROGRESS) that can be invoked from doInBackground method by calling publishProgress(YOUR_PROGRESS)
the data type of YOUR_PROGRESS can be defined from AsyncTask<Int, YOUR_PROGRESS_DATA_TYPE, Long>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文