开发者

Progress Dialog in Android doesn't Show?

开发者 https://www.devze.com 2022-12-31 21:31 出处:网络
Okay.. I am doing something similar to the below: private void onCrea开发者_运维百科te() { final ProgressDialog dialog = ProgressDialog.show(this, \"Please wait..\", \"Doing stuff..\", true);

Okay.. I am doing something similar to the below:

private void onCrea开发者_运维百科te() {
    final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
Thread t = new Thread() {
    public void run() {
        //do some serious stuff...
        dialog.dismiss();           
    }
};
t.start(); 
t.join();
stepTwo();

}

However, what I am finding is that my progress dialog never even shows up. My App stalls for a moment so I know it is chugging along inside of thread t, but why doesnt my dialog appear?

IF I remove the line:

t.join();

Then what I find happens is that the progress dialog does show up, but my app starts stepTwo(); before what happens in the thread is complete..

Any ideas?


Try to use an handler

public class MyActivity {

private Handler handler;

private void onCreate() {

   handler = new Handler() {

      @Override
      public void handleMessage(Message msg) {
         pd.dismiss();
         stepTwo();
      }
   };

   final ProgressDialog dialog = ProgressDialog.show(this, "Please wait..", "Doing stuff..", true);
   Thread t = new MyThread() {
   t.start():

}

private class MyThread extends Thread() {
   public void run() {
      //do some serious stuff...
      handler.sendEmptyMessage(0);       
   }
}   

}


Your join() line blocks the UI thread that runs the ProgressDialog. You are therefore blocking layouts, drawings, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消