android: showing a progress dialog
Asked Answered
E

2

8

I have looked at the Android API and other posts here on stackoverflow, but have not been able to figure this out.

My app downloads files to the sd card. I would like to pop a "loading..." dialog while the file is downloading and then have it disappear when the download is finished. This is what i came up with using the android API:

ProgressDialog pd = ProgressDialog.show(this,"","Loading. Please wait...",true);

//download file

pd.cancel();

however the dialog doesn't actually show. when i debug it, it claims that it is showing, but it is obviously not on the screen.

what can i do?

Enravish answered 10/6, 2010 at 18:36 Comment(2)
does the download code run into a separated thread?Sketchy
no it runs on the same thread as the activity.Enravish
S
3

You have to run the download code into a separated Thread. The easy way is to use AsyncTask

Look also this article on how to use it

Sketchy answered 10/6, 2010 at 20:12 Comment(1)
i actually had a meeting where we talked about threading and i figured it out. i also had to put it into a separate activity so my app wouldnt try to load the file before the download activity finished. thanks for your help! you got the wheels turning.Enravish
R
2

I have a somewhat similar scenario, more of a level loading pause while stuff is being setup for a phase of a game. I launch the dialog, start level loading in a thread which is passed the context and call this at the end of the thread:

mContext.runOnUiThread(new Runnable(){
    public void run() {
        mContext.mProgressDialog.dismiss();
    }
});

I find the runOnUiThread() method extremely useful.

Reachmedown answered 10/6, 2010 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.