Reputation: 3060
I'd like to do the following in the right order: - Do a long task while showing a progress dialog - And then use the result of this calculation.
However, to show a progress dialog I seem to be forced to use a Thread. But if I use a thread, the calculation is done before my long task is finished.
What is the solution ?
Both solutions that were proposed unfortunately don't work for me. I will try to be more thorough. Taking ProfSmiles' code, here is what I'd like:
final Handler mHandler = new Handler(); //create the handler
String mResults; //the results of the operation (change the type if needbe)
final Runnable mDoSomething = new Runnable()
{
public void run()
{
returnResultsToUi(); //what happens when it's done
}
};
protected void doActionBackground()
{
t = new Thread()
{
public void run()
{
mResults = doSomethingThatTakesALongTime(); //the backbone of this entire thing
mHandler.post(mDoSomething);
}
};
t.start();
}
public void returnResultsToUi()
{
//you can call UI ops safely in here
try
{
//close the spinner
loading.setProgress(1);
loading.dismiss();
}
catch (Exception e)
{
Log.w("error","couldn't close loading properly");
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loading = new ProgressDialog(this); //create and then set up the spinner
loading.setMax(1);
loading.setTitle("Loading...");
loading.setMessage("Doing something.");
loading.setCancelable(true);
loading.setIndeterminate(false);
loading.setOnCancelListener(cancelLoad()); //remember to make this method or change setCancelable(false)
loading.setProgress(0);
loading.show();
doActionBackground(); //start the background process
doSomethingAfter(mResults);
The problem is that doSomethingAfter is called while doActionBackground is running, but mResults is not set yet and its value is null. Is there a way to force the thread to finish before calling doSomethingAfter? Like a function waitTillFinished(takesALongTime) that can be cancelled.
Upvotes: 0
Views: 323
Reputation: 1406
You can use Handlers to do long-running processes in the background and return the result to the UI thread. For example;
final Handler mHandler = new Handler(); //create the handler
String mResults; //the results of the operation (change the type if needbe)
final Runnable mDoSomething = new Runnable()
{
public void run()
{
returnResultsToUi(); //what happens when it's done
}
};
protected void doActionBackground()
{
t = new Thread()
{
public void run()
{
mResults = doSomethingThatTakesALongTime(); //the backbone of this entire thing
mHandler.post(mDoSomething);
}
};
t.start();
}
public void returnResultsToUi()
{
//you can call UI ops safely in here
try
{
//close the spinner
loading.setProgress(1);
loading.dismiss();
}
catch (Exception e)
{
Log.w("error","couldn't close loading properly");
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loading = new ProgressDialog(this); //create and then set up the spinner
loading.setMax(1);
loading.setTitle("Loading...");
loading.setMessage("Doing something.");
loading.setCancelable(true);
loading.setIndeterminate(false);
loading.setOnCancelListener(cancelLoad()); //remember to make this method or change setCancelable(false)
loading.setProgress(0);
loading.show();
doActionBackground(); //start the background process
Upvotes: 0
Reputation: 31463
The solution is to use AsyncTask with ProgressBar.
+1 yes this is the exact solution in android to implement Threading concept. Implement the below scenario to implement your solution:
The best example suits my explanation is here: http://www.technotalkative.com/android-load-image-from-web/ , sorry for my personal blog link but i am sure this helps user to understand and implement the AsyncTask concept.
Upvotes: 3