开发者

Android: ProgressDialog failing, Version 2

I asked a similar Q to this before but I'm still in trouble. ProgressDialog is causing me severe hair loss. This code has a progressdialog (spinner) just to see if I can make one appear - which just causes a 10 second delay before I then go and open my file.

I have a listactivity. The user selects a file, which brings us here:

private void openFile(File f) {
    Global.iDelay = 0;

    // Create an Intent
    Global.file = f;

    dialog = ProgressDialog.show(viewer2.this, "", "Loading...");
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.show(); // dialog does not appear //

    // create a thread for counting the sleep seconds
    Thread background = new Thread (new Runnable() {
       public void run() {
           try {

              while (Global.iDelay < 10) {
                   开发者_高级运维// wait between each update
                   Thread.sleep(1000);

                   Log.d("Baz","iDelay = "+Global.iDelay);

                   // active the update handler
                   progressHandler.sendMessage(progressHandler.obtainMessage());
                   }

           } catch (java.lang.InterruptedException e) {
               // if something fails do something smart
           }
       }
    });

    // start the background thread
    background.start();

    dialog.dismiss();

    //--------------------------------------------------------------
    // then I go and prepare for my file and open it        


    Global.PrepareForGL (this); 

    Intent myIntent1 = new Intent(this, ShowGL.class);
    this.startActivity(myIntent1);
    }

My handler is defined like this:

//handler for the background updating
public static Handler progressHandler = new Handler() {
    public void handleMessage(Message msg) {
        Global.iDelay++;
    }
};

My problem: The timer thread starts and suire enough counts off 10 seconds (my log.d gives me a count to 10), but no dialog appears. However, during the 10 second count I am already executing the 'prepareforgl' method, which I understood would not happen until the background thread has completed and the dialog been dismissed. I know this because that also contains a log.d line which gives me a result between the 5 and 6 second counters.

My Questions: Is it relevant that I'm doing this in a listactivity? Where is my dialog? Am I right in thinking the entire background thread should have executed before I then carry on with my work?

Any light you can shed on progressdialogs would be very welcome. Many thanks.

Baz.


You are starting your background thread and then dismissing the dialog. So, you aren't actually delaying the call to dismiss the dialog. It might just be happening too fast for you to ever see it.

Your code would be much cleaner if you use AsyncTask to do this. Put the delay in the doInBackground method, and the dialog dismissal in onPostExecute.

Also, you are mixing the static call to create and show the dialog with other calls to change and re-display the dialog. Try this instead:

dialog = new ProgressDialog(viewer2.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
dialog.show();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜