我知道這裏有類似的問題,但我仍然沒有找到答案。問題是這個長時間操作的進度對話框不會顯示出來,但仍然在處理完成。我認爲上下文有問題,但不知道如何解決這個問題。AsyncTask中的進度對話框不顯示
public class MainActivity extends Activity {
Utilities uti = new Utilities();
SharedPreferences prefs = null;
private ContactServiceActivity contactService;
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactService = new ContactServiceActivity(getApplicationContext());
doFirstRun();
Intent i = new Intent(getBaseContext(), ContactListActivity.class);
startActivity(i);
}
private void doFirstRun() {
SharedPreferences settings = getSharedPreferences("pl.stxnext.stxcontactsync", MODE_PRIVATE);
if (settings.getBoolean("isFirstRun", true)) {
new firstRunTask().execute();
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("isFirstRun", false);
editor.commit();
}
}
private class firstRunTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Trwa synchronizacja danych");
mProgressDialog.setMessage("Może to zająć chwilę, proszę czekać.");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
contactService.getAssetsAtFirstRun();
return null;
}
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
uti.showToast(getBaseContext(), "Zapisano kontakty.");
}
}
}
嘗試this..http://stackoverflow.com/questions/17585759/cant-dismiss-progressdialog-after-the-asynctask-complete/17585792#17585792 – TheFlash