我在我的活动中调用了一个函数,这基本上是一个通过 web 服务从远程服务器获取数据的异步任务。
private void myAsyncTask() {
new AsyncTask<Object, Object, Object>() {
@Override
protected void onPreExecute() {
progress_Dialog = ProgressDialog.show(a, "", "Loading");
}
@Override
protected Integer doInBackground(Object... params) {
try
{
try {
MenuService menuService = new MenuServiceImpl();
MenuServiceResponse partnerMenu;
partnerMenu = menuService.getMenu();
productlist=Menu.getMenu().getMenuEntries();
System.gc();
return 0;
} catch (myServiceException e) {
bgFlag=true;
e.printStackTrace();
}
}
catch (Exception e) {
bgFlagForserviceExeption=true;
e.printStackTrace();
}
} return 0;
}
@Override
protected void onPostExecute(Object result) {
if (progress_Dialog != null) {
progress_Dialog.dismiss();
}
try
{
if(bgFlagForserviceExeption)
{
MyAlertDialog.ShowAlertDialog(ShopActivity.this, "", "Please try again later", "OK");
}
if(bgFlag==false)
{
adapter = new ShopAdapter(
ShopActivity.this, productlist);
allproduts.setAdapter(adapter);
}
else
{
MyAlertDialog.ShowAlertDialog(ShopActivity.this, "", " Please try again later", "OK");
}
}
catch(Exception e)
{
adapter=null;
}
}
}.execute();
}
当我调用此活动时,进度条将一直显示,直到 doinbackground() 结束。我如何在按下手机后退按钮时退出后台进程,现在的问题是我必须等到后台进程完成,即使我按后退按钮。我怎样才能做到这一点