在Asyn任务执行时阻止android后退按钮
当我的应用程序启动时,我正在调用 Asyn Task 函数。在后台进程中,它会命中 3 个 API 并继续解析它们的功能,大约需要 10 分钟的时间。
在这种情况下,如果用户单击 Android 默认后退按钮,则异步任务将被取消。为了避免这种情况,我尝试阻止后退按钮操作,如下所示。
class First_Time_Sync extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog1 = new ProgressDialog(Progressss.this);
protected void onPreExecute()
{
backMode = true;
dialog1.setMessage("Welcome \nThank you for waiting.");
dialog1.setIndeterminate(true);
dialog1.show();
}
protected Void doInBackground(Void... arg0)
{
.................
return null;
}
protected void onPostExecute(Void unused)
{
backMode = false;
dialog1.dismiss();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (backMode)
{
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}
}
return super.onKeyDown(keyCode, event);
}
现在,当调用异步任务函数并且用户单击后退按钮时,进度条将被取消。但后台进程仍在继续,并且后退按钮被阻止,直到后台进程完成。
我希望即使按下后退按钮也能显示进度条,这样它就不会消失。
这要怎么做......
When my app gets launched i am calling a Asyn Task function. In the background process its hitting 3 APIs and their parsing functions goes on, It will be taking around 10 minutes of time.
In that case if the user clicks on the android default back button, then the asyn task used to get cancelled. To avoid this i tried blocking the back button action as follows.
class First_Time_Sync extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog1 = new ProgressDialog(Progressss.this);
protected void onPreExecute()
{
backMode = true;
dialog1.setMessage("Welcome \nThank you for waiting.");
dialog1.setIndeterminate(true);
dialog1.show();
}
protected Void doInBackground(Void... arg0)
{
.................
return null;
}
protected void onPostExecute(Void unused)
{
backMode = false;
dialog1.dismiss();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (backMode)
{
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}
}
return super.onKeyDown(keyCode, event);
}
Now when the async task function called and if the user clicks back button progress bar gets cancelled. But the background process goes on and the back button is been blocked until the background process completes.
I want the progress bar to be shown even if the back button is pressed, so that it may not get vanished.
How to do this......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将对话框的可取消属性设置为 false
这将防止后退键取消对话框
You will have to set the cancelable property of the dialog to false
This will prevent back key from cancelling your dialog