Created
May 30, 2012 14:12
-
-
Save samthor/2836588 to your computer and use it in GitHub Desktop.
Handler-compatible AsyncHandler which runs tasks on some other thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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