dimabin
dimabin

Reputation: 107

Checking the completion of Android background thread

After loading the first activity, my application immediately starts downloading some files in a background process (I use new Thread() for this).

In the first activity, there is also a button which opens the second activity. It is crucial for the second activity that the background download is finished.

My task:

If user clicks on the button while the background download is not yet finished, the application must wait and not open the second activity. It must show a message like "Please wait" instead.
If in XX seconds after clicking download is not finished either, another message (with OK button) appears: "You have a too slow connection."

Upvotes: 0

Views: 399

Answers (3)

Zombie
Zombie

Reputation: 404

You need to use AsyncTask for this. Using its override methods.

Upvotes: 0

Frank Sposaro
Frank Sposaro

Reputation: 8531

Have your thread broadcast a "completed" action to the main thread and stash this result. Then on the UI thread you can check this variable to proceed.

Upvotes: 1

Tomer Shemesh
Tomer Shemesh

Reputation: 13405

you can check using

Thread myThread; //your thread that is downloading thigns
if(myThread.isAlive())
{
    //do what you want here
}

Upvotes: 0

Related Questions