Building a Linux Media Network, one step at a time

Wednesday, July 08, 2009

Android SwingWorker

I've been fooling around with the Android development platform. It's quite an adjustment, coming from the iPhone world. More thoughts to follow, although probably on a different blog. I'm getting sick of this blogger nonsense.

Anyways, I thought I'd post a simple equivalent to the SwingWorker class that works with Android's Event Dispatch Thread. Here it is:


package ca.razorwire.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public abstract class UIWorker
{
private static final ExecutorService __execSvc;
private static final Handler __edtHandler;

private static final String TAG = "UIWorker";

static
{
__execSvc = Executors.newSingleThreadExecutor();
__edtHandler = new Handler( Looper.getMainLooper() );
}

public abstract void doInBackground();

public void done()
{
// Log.d(TAG, "done() executing in thread " + Thread.currentThread().getName() );
// This space intentionally left blank
}

public void execute()
{
// Log.d(TAG, "execute() executing in thread " + Thread.currentThread().getName() );
final Runnable doneRunner = new Runnable()
{
public void run()
{
UIWorker.this.done();
}
};

final Runnable bgRunner = new Runnable()
{
public void run()
{
// Log.d(TAG, "bgRunner executing in thread " + Thread.currentThread().getName() );
UIWorker.this.doInBackground();
__edtHandler.post( doneRunner );
}
};
__execSvc.submit(bgRunner);
}
}


And here's a sample usage:


UIWorker worker = new UIWorker()
{
public void doInBackground()
{
Log.d( TAG, "Sleeping for a bit." );
try { Thread.sleep( 10000 ); } catch ( Exception ignore ) {};
}
};
Log.d( TAG, "Executing worker..." );
worker.execute();
Log.d( TAG, "Returned from execute()" );


If you uncomment the Log calls in UIWorker, you should see some output like this:


07-08 12:14:11.015: DEBUG/rweeks(1078): Executing worker...
07-08 12:14:11.015: DEBUG/UIWorker(1078): execute() executing in threadmain
07-08 12:14:11.025: DEBUG/UIWorker(1078): bgRunner executing in thread pool-1-thread-1
07-08 12:14:11.035: DEBUG/rweeks(1078): Sleeping for a bit.
07-08 12:14:11.035: DEBUG/rweeks(1078): Returned from execute()
07-08 12:14:21.038: DEBUG/UIWorker(1078): done() executing in thread main


I'm 100% sure that Blogger is going to screw up the formatting.

Public domain code, no guarantees, seems to work.