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 で非表示にします。