在 AsyncTask 中显示 AlertDialog 的奇怪效果:如果在AsyncTask 执行期间应用程序被最小化:
private class CheckDeviceConfiguration extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(ActivitySIPCountrySelection.this, "Title", "working...", true);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressDialog.dismiss(); //hide progress dialog previously shown
if (!result) {
AlertDialog.Builder dialog = new AlertDialog.Builder(ActivitySIPCountrySelection.this);
dialog.setCancelable(false);
dialog.setMessage("Message");
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int arg1) {
//do something
}
});
dialog.show();
}
}
@Override
protected Boolean doInBackground(Void... params)
Thread.sleep(5000);
return false;
}
}
如果我单击我的应用程序图标进行恢复,UI 没有响应并且活动看起来有点暗(不活动?)。后退按钮无效。
编辑:
有人问我在哪里调用 AsyncTask。好吧,来自Activity onCreate()。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sip_country_selection);
new CheckDeviceConfiguration().execute();
}
异步任务正确显示进度对话框并将其隐藏在 onPostExecute 中。