User interface responsiveness is a crucial thing for all applications
that require user interaction, but maybe even more so when programming for mobile
handsets. Google’s Android
programming environment unfortunately does not provide any mechanism for handling
operations that may require a fair amount of time to complete, but which in itself
are not meant to be implemented as
Android Services. An example for this would be network I/O in an
Activity, such as posting data to (or retrieving data from) a remote Web server.
Because the Android runtime will terminate any Activity that does not respond within
a couple of seconds, it is impossible (and simply a bad idea anyway) to perform
such tasks from within the UI main thread.
That being said, I have come up with a generic class
that handles long running operations by spawning a separate thread and passing
back any result or error data to the main thread using a callback mechanism.
An animated progress dialog will be displayed while the operation is running.
That way the user is kept informed about any program activity that may take
some time to complete.
The class can be used as follows:
- public
class MyActivity
implements LongRunningActionCallback<Void>
{
-
- private
LongRunningActionDispatcher<Void> dispatcher;
-
- private
void startLongRunningOperation() {
-
-
-
-
this.dispatcher =
new LongRunningActionDispatcher<Void>(this,
this);
- dispatcher.startLongRunningAction(new
Callable<Void>() {
-
public Void call()
throws Exception {
-
-
return null;
-
}
- },
"Dialog Title",
"Dialog message");
- }
-
-
- public
void onLongRunningActionFinished(Void
result, Exception error) {
-
if (error !=
null) {
-
- }
else {
-
- }
- }
- }
This will spawn a progress dialog (not indicating any
actual progress in percentage, it’s just a “busy” dialog) with the given
title and message. If an exception occurred in the Callable you provided,
it will be passed as the error argument to the callback, so you should always
check whether it’s non-null.
Below are the source codes for both LongRunningActionDispatcher and LongRunningActionCallback.
- import java.util.concurrent.Callable;
-
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.os.Handler;
- import android.util.Log;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public
final class
LongRunningActionDispatcher<ResultType> {
-
- private
Context context;
-
- private
LongRunningActionCallback<ResultType> callback;
-
-
-
-
- private
ProgressDialog progressDialog;
-
- private
Handler finishedHandler = new Handler();
-
- public
LongRunningActionDispatcher(Context context,
-
LongRunningActionCallback<ResultType> callback) {
-
this.context = context;
-
this.callback = callback;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public
void startLongRunningAction(final
Callable<ResultType> callable,
-
String progressDialogTitle, String progressDialogMessage) {
-
- progressDialog
= ProgressDialog.show(context, progressDialogTitle,
-
progressDialogMessage, true,
false);
-
-
new Thread(new
Runnable() {
-
-
public void
run() {
-
ResultType result = null;
-
Exception error = null;
-
try {
-
result = callable.call();
-
} catch (Exception e) {
-
Log.e("ERROR", e.getMessage());
-
error = e;
-
}
-
-
final ResultType finalResult = result;
-
final Exception finalError = error;
-
finishedHandler.post(new Runnable()
{
-
-
public void
run() {
-
onLongRunningActionFinished(finalResult, finalError);
-
}
-
});
-
}
- }).start();
- }
-
- private
void onLongRunningActionFinished(ResultType
result, Exception error) {
- progressDialog.dismiss();
- callback.onLongRunningActionFinished(result,
error);
- }
- }
-
-
-
-
-
-
-
-
-
- public
interface LongRunningActionCallback<ResultType>
{
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void
onLongRunningActionFinished(ResultType result, Exception error);
- }
How to use custom de...
lets say i change this app to days to...
How to display a JPG...
Is that posible to zoom in and drag p...
Making a custom Andr...
Cool - I will use it carefully i promise
How to display a JPG...
problem in image loading - i have exe...
Introducing Calculon...
Thank You for your contribution - Hi,...