我有一个 AsyncTask,它在工作时显示一个 progressDialog(它从 doInBackground 中调用 runOnUiThread 来显示进度对话框).
I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).
在运行时,我想允许使用后退按钮取消操作;其他人遇到了这个问题:返回按钮不起作用,而progressDialog 正在运行
Whilst its running I want to allow the use of the back button to cancel the operation; someone else has had this problem: BACK Button is not working ,while progressDialog is running
由于什么原因我无法回复该主题,因此不得不开始另一个?!(另一天的另一个问题)
For what ever reason I can't reply to that thread, hence having to start another?! (Another question for another day)
我和 Sandy 有同样的想法,但是当 progressDialog 显示时,这段代码从未被调用,这是为什么呢?我已经在我的主要活动类中实现了它,progressDialog 是否暂时将前台焦点从我的类中移开?
I had the same idea as Sandy but this code is never called whilst the progressDialog is showing, why is this? I have implemented it inside my main activity class, does the progressDialog take the foreground focus away from my class temporarily?
首先,您应该从 OnPreExecute
显示您的对话框,将其隐藏在 OnPostExecute
中,并且 - 如有必要- 通过发布进度修改它.(参见这里)
First, you should show your dialog from OnPreExecute
, hide it in OnPostExecute
, and - if necessary - modify it by publishing progress. (see here)
现在回答您的问题:ProgressDialog.show()
可以将 OnCancelListener
作为参数.您应该提供一个在进度对话框实例上调用 cancel()
的方法.
Now to your question: ProgressDialog.show()
can take a OnCancelListener
as an argument. You should provide one that calls cancel()
on the progress dialog instance.
示例:
@Override
protected void onPreExecute(){
_progressDialog = ProgressDialog.show(
YourActivity.this,
"Title",
"Message",
true,
true,
new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
YourTask.this.cancel(true);
finish();
}
}
);
}
其中 _progressDialog
是 YourTask
的 ProgressDialog
成员.
where _progressDialog
is a ProgressDialog
member of YourTask
.
此类在 API 级别 26 中已弃用.ProgressDialog 是一种模式对话框,它会阻止用户与应用程序交互.反而使用此类,您应该使用进度指示器,例如ProgressBar,可以嵌入到应用程序的 UI 中.或者,您可以使用通知来通知用户任务的进度.链接
This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress. LINK
这篇关于Android 后退按钮和进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!