从 AsyncTask 更新 Activity 中的进度对话框
在我的应用程序中,我按照 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 duringdoInBackground()
by callingpublishProgress()
.Refer to the docs for more details
您可以从 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 fromdoInBackground
method by callingpublishProgress(YOUR_PROGRESS)
the data type of YOUR_PROGRESS can be defined from
AsyncTask<Int, YOUR_PROGRESS_DATA_TYPE, Long>