Android:ProgressDialog未在onCreate()中调用的AsyncTask中显示
我有一个AsyncTask,它将在Android:ProgressDialog未在onCreate()中调用的AsyncTask中显示,android,android-asynctask,android-progressbar,Android,Android Asynctask,Android Progressbar,我有一个AsyncTask,它将在onCreate方法中执行。但是,我的ProgressDialog没有出现。通过调试,确认正在执行AsyncTask @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lifestyle); co
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();
}
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和生活方式);
context=getApplicationContext();
newtestasync().execute();
}
私有类testAsync扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newProgressDialog(MainActivity.this);//使用上下文进行了尝试,没有区别
pDialog.setTitle(“插入样本数据”);
pDialog.setMessage(“请稍候。此对话框将在完成后关闭”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TableControllerReadings TCR=新的TableControllerReadings(上下文);
//insertSampleData(getApplicationContext());
新建计时器()。计划(新建计时器任务()){
@凌驾
公开募捐{
//延迟5秒
}
}, 5000);
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
pDialog.disclose();
}
}
活动在调用onResume
方法之前不会显示任何视图。如果您的AsyncTask
执行在onResume
调用之前完成,那么您将永远不会看到ProgressDialog
所以最好调用
newtestasync().execute()在调用onResume
方法之前,活动中的code>不会显示任何视图。如果您的AsyncTask
执行在onResume
调用之前完成,那么您将永远不会看到ProgressDialog
所以最好调用newtestasync().execute()
在onResume
使用你的pDialog.disease()将>编码到计时器线程中
原因:onPostExecute()
立即调用,因为后台任务已完成
它的seprate线程处于延迟状态,因此光标移动到onPostExecute()
私有类testAsync扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newProgressDialog(MainActivity.this);//使用上下文进行了尝试,没有区别
pDialog.setTitle(“插入样本数据”);
pDialog.setMessage(“请稍候。此对话框将在完成后关闭”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TableControllerReadings TCR=新的TableControllerReadings(上下文);
//insertSampleData(getApplicationContext());
新建计时器()。计划(新建计时器任务()){
@凌驾
公开募捐{
//延迟5秒
pDialog.disclose();
}
}, 5000);
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
//pDialog.disclose();
}
}
拿起你的pDialog.disclose()将>编码到计时器线程中
原因:onPostExecute()
立即调用,因为后台任务已完成
它的seprate线程处于延迟状态,因此光标移动到onPostExecute()
私有类testAsync扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newProgressDialog(MainActivity.this);//使用上下文进行了尝试,没有区别
pDialog.setTitle(“插入样本数据”);
pDialog.setMessage(“请稍候。此对话框将在完成后关闭”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TableControllerReadings TCR=新的TableControllerReadings(上下文);
//insertSampleData(getApplicationContext());
新建计时器()。计划(新建计时器任务()){
@凌驾
公开募捐{
//延迟5秒
pDialog.disclose();
}
}, 5000);
返回null;
}
@凌驾
受保护的void onPostExecute(void v){
//pDialog.disclose();
}
}
添加pDialog.show()代码>在doInBackground中添加newtestasync().execute()代码>在onResume中添加pDialog.show()代码>在doInBackground中添加newtestasync().execute()代码>在onResume中您的代码与线程配合良好。睡眠(5000);如果要添加延迟,请使用Thread.Sleep(/time in milisec/)而不是Timer.Schedule。由于计时器的原因。Schedule异步任务在显示任何对话框之前执行得非常快。您的代码在Thread.sleep(5000)上运行良好;如果要添加延迟,请使用Thread.Sleep(/time in milisec/)而不是Timer.Schedule。由于计时器的原因。Schedule async任务在显示任何对话框之前执行得非常快。必须在UI线程中调用.show()
方法。在doInBackground
中校准它会让我
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();
}
}