Skip to content

Instantly share code, notes, and snippets.

@samthor
Created May 30, 2012 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samthor/2836588 to your computer and use it in GitHub Desktop.
Save samthor/2836588 to your computer and use it in GitHub Desktop.
Handler-compatible AsyncHandler which runs tasks on some other thread
package foo;
import android.os.Handler;
import android.os.HandlerThread;
/**
* Subclass of {@link Handler} that does things on some background thread.
*
* @author Sam Thorogood (sam.thorogood@gmail.com)
*/
public class AsyncHandler extends Handler {
private final HandlerThread thread;
public static Handler create() {
HandlerThread thread = new HandlerThread(AsyncHandler.class.getName());
thread.start();
return new AsyncHandler(thread);
}
private AsyncHandler(HandlerThread thread) {
super(thread.getLooper());
this.thread = thread;
}
@Override
public boolean equals(Object o) {
if (o instanceof AsyncHandler) {
return ((AsyncHandler) o).thread.equals(thread);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment