我想在后台执行一些任务AsynTask
。如下图所示:取消与后退按钮的asynctask
class CheckTypesTask extends AsyncTask<Void, Void, Void> {
ProgressDialog Asycdialog = new ProgressDialog(Home.this);
String typeStatus;
@Override
protected void onPreExecute() {
super.onPreExecute();
Asycdialog.setMessage("First time initialization. Please wait...");
Asycdialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
create_database_and_tables();
insert_in_questions();
insert_in_answer_option();
return null;
}
@Override
protected void onPostExecute(Void result) {
// hide the dialog
super.onPostExecute(result);
database.close();
Asycdialog.dismiss();
}
现在的问题是,如果我按硬盘返回键,而ProgressDialog
是可见的,它只是简单地关闭ProgressDialog
。但是,当我检查Database
时,我发现所有要插入的记录都已成功插入。我不明白为什么ProgressDialog
继续显示,即使doInBackground()
完成其工作,无论ProgressDialog
在某个时间后消失,但为什么这样?
我的下一个问题是,如果要插入的数据集不够大,那么为什么仍然出现ProgressDialog
。其实,我是这样测试的。两秒钟后立即按下硬锁键。并检查数据库,我发现所有的记录都被插入。为什么只要doInBackground()
完成其工作就不会被呼叫?
最后一个问题是,我想显示一个AlertDialog
与是和不Button
,当硬按键被按下,而ProgressDialog
仍然可见。然后,如果按下,则应该消失ProgressDialog
,并且应该停止doInBackground()
工作。如果没有被按下,那么只有AlertDialog
应该被解雇,并且ProgressDialog
应该仍然可见。
请帮我解决我的疑惑。
看看我的这个答案是否有帮助:http://stackoverflow.com/a/15947770/450534 – 2013-04-24 08:21:33