AsyncTask 无法与 Android 中的 ProgressDialog 一起使用
我正在使用 asynctask
从网站下载数据并且 我的异步任务代码位于
public class getSyncTaskInBackground extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
getSynchronizeTask();
return null;
}
@Override
protected void onPostExecute(Void result) {
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
displaySynchronizetask();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(synchronize.this, "Tasks are synchroning...", "Please wait...");
super.onPreExecute();
}
@Override
protected void onCancelled() {
super.onCancelled();
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
此线程下方,需要更多时间才能完成任务 所以如果我想在进程之间取消这个线程,那么我已经编写了按下后退按钮的代码 或者如果进度条已经关闭,那么我想通过调用finish();来关闭活动 我的后退按钮代码如下
@Override
public void onBackPressed() {
syncThread.cancel(true); //syncThread is the object of getSyncTaskInBackground
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
} else {
finish();
}
}
现在,当我按下后退按钮时,progressdialog
不会关闭 mycode 有什么错误吗?有什么办法可以完成我的需求吗? 请帮我
i am downloading data from website using asynctask
and
my code for async task is below
public class getSyncTaskInBackground extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
getSynchronizeTask();
return null;
}
@Override
protected void onPostExecute(Void result) {
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
displaySynchronizetask();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(synchronize.this, "Tasks are synchroning...", "Please wait...");
super.onPreExecute();
}
@Override
protected void onCancelled() {
super.onCancelled();
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
this thread takes more time to complete task
so if i want to cancel this thread in between of process then i had write this code of back button pressed
or if progressbar is already dismiss then i want to close activity by calling finish();
my code for back button is as below
@Override
public void onBackPressed() {
syncThread.cancel(true); //syncThread is the object of getSyncTaskInBackground
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
} else {
finish();
}
}
Now when i pressed back button then the progressdialog
is not dismiss
is any mistake in mycode ? is any way to complete my need ?
please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗨
您可能想打电话
在您的 ProgressDialog 实例上将 setCancelable 设置为 true :
http://developer.android.com/reference/android/app /Dialog.html#setCancelable(boolean)
如果您想要取消进度对话框的事件
你可以设置一个 onCancelListener
http://developer.android.com /reference/android/app/Dialog.html#setOnCancelListener(android.content.DialogInterface.OnCancelListener)
希望这有帮助
Hi
You might want to call
setCancelable with true on your progressDialog instance :
http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)
If you want an event on the cancelation of your progressDialog
you can set an onCancelListener
http://developer.android.com/reference/android/app/Dialog.html#setOnCancelListener(android.content.DialogInterface.OnCancelListener)
hope this helps