Android:在 onCreate() 中调用的 AsyncTask 中未显示 ProgressDialog
Android: ProgressDialog not showing in AsyncTask called in onCreate()
我有一个 AsyncTask 将在 onCreate
方法中执行。但是,我的 ProgressDialog 没有出现。并且通过调试,确认正在执行AsyncTask
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifestyle);
context = getApplicationContext();
new testAsync().execute();
}
private class testAsync extends AsyncTask<Void,Void,Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
pDialog.setTitle("Inserting sample data");
pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TableControllerReadings TCR = new TableControllerReadings(context);
// TCR.insertSampleData(getApplicationContext());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//delay for 5 seconds
}
}, 5000);
return null;
}
@Override
protected void onPostExecute(Void v) {
pDialog.dismiss();
}
}
在调用 onResume
方法之前,Activity 不会显示任何视图。如果您的 AsyncTask
执行在 onResume
调用之前完成,那么您将永远不会看到 ProgressDialog
。
所以最好在 onResume
中调用 new testAsync().execute();
将您的 pDialog.dismiss();
放入您的计时器线程。
原因: onPostExecute()
立即调用,因为后台任务已完成。
它的单独线程处于延迟状态,因此光标移动到 onPostExecute()
private class testAsync extends AsyncTask<Void, Void, Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
pDialog.setTitle("Inserting sample data");
pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TableControllerReadings TCR = new TableControllerReadings(context);
// TCR.insertSampleData(getApplicationContext());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//delay for 5 seconds
pDialog.dismiss();
}
}, 5000);
return null;
}
@Override
protected void onPostExecute(Void v) {
// pDialog.dismiss();
}
}
在doInBackground中添加pDialog.show();
并在onResume中添加new testAsync().execute();
您的代码在 Thread.sleep(5000) 下运行良好;如果您想添加延迟,请使用 Thread.Sleep(/time in milisec/) 而不是 Timer.Schedule。因为 Timer.Schedule 异步任务在显示任何对话框之前执行得非常快。
我有一个 AsyncTask 将在 onCreate
方法中执行。但是,我的 ProgressDialog 没有出现。并且通过调试,确认正在执行AsyncTask
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifestyle);
context = getApplicationContext();
new testAsync().execute();
}
private class testAsync extends AsyncTask<Void,Void,Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
pDialog.setTitle("Inserting sample data");
pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TableControllerReadings TCR = new TableControllerReadings(context);
// TCR.insertSampleData(getApplicationContext());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//delay for 5 seconds
}
}, 5000);
return null;
}
@Override
protected void onPostExecute(Void v) {
pDialog.dismiss();
}
}
onResume
方法之前,Activity 不会显示任何视图。如果您的 AsyncTask
执行在 onResume
调用之前完成,那么您将永远不会看到 ProgressDialog
。
所以最好在 onResume
new testAsync().execute();
将您的 pDialog.dismiss();
放入您的计时器线程。
原因: onPostExecute()
立即调用,因为后台任务已完成。
它的单独线程处于延迟状态,因此光标移动到 onPostExecute()
private class testAsync extends AsyncTask<Void, Void, Void> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this); // tried with context, no difference
pDialog.setTitle("Inserting sample data");
pDialog.setMessage("Please wait. This dialog will be dismissed upon completion.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TableControllerReadings TCR = new TableControllerReadings(context);
// TCR.insertSampleData(getApplicationContext());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//delay for 5 seconds
pDialog.dismiss();
}
}, 5000);
return null;
}
@Override
protected void onPostExecute(Void v) {
// pDialog.dismiss();
}
}
在doInBackground中添加pDialog.show();
并在onResume中添加new testAsync().execute();
您的代码在 Thread.sleep(5000) 下运行良好;如果您想添加延迟,请使用 Thread.Sleep(/time in milisec/) 而不是 Timer.Schedule。因为 Timer.Schedule 异步任务在显示任何对话框之前执行得非常快。