I have an application which runs a long task and returns a value. While the task is running, a ProgressDialog shows the progress. After the task is done I want to show the result in a TextView. I run the task in a FutureTask.
My problem is that if I try to get the result, the .get() method of FutureTask blocks the UI Thread and I don’t see the ProgressDialog (the TextView displays the result propertly).
My code for the task (pool is ExecutorService):
final FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
@Override
public String call() {
return myLongTask();
}
});
pool.execute(future);
And afterwards I call updateProgressBar() in a Runnable which updates the ProgressDialog with a Handler:
Runnable pb = new Runnable() {
public void run() {
myUpdateProgressBar();
}
};
pool.execute(pb);
Now I’m getting the result, which blocks the UI Thread preventing the ProgressDialog to show up:
String result = future.get()
If I try to put the result inside the updateProgressBar() method (by passing the future as a parameter) after the ProgressDialog dismisses, an Exception is thrown:
Only the original thread that created a view hierarchy can touch its views.
Any ideas how to solve this problem? (I’ve heard about AsyncTasks but I can’t figure out how to use them propertly.)
You are correct that you need either an
AsyncTaskor aThread/Handlercombination in order to not block the UI.Neither approach is that tricky, and there are some good guides around that lay out how to do them. Here are some links that I’d recommend.