Last active
April 10, 2016 01:41
-
-
Save cavarzan/bd3c2c027c77742c43510adac981a65b to your computer and use it in GitHub Desktop.
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
import android.os.Handler; | |
import android.os.HandlerThread; | |
import android.os.Looper; | |
import android.os.Message; | |
import android.os.Process; | |
import java.util.concurrent.Semaphore; | |
public class CustomHandler extends Handler { | |
public CustomHandler(String name) { | |
this(name, Process.THREAD_PRIORITY_BACKGROUND); | |
} | |
protected CustomHandler(String handlerName, int handlerPriority) { | |
super(startHandlerThread(handlerName, handlerPriority)); | |
} | |
private static Looper startHandlerThread(String name, int priority) { | |
final Semaphore semaphore = new Semaphore(0); | |
HandlerThread handlerThread = new HandlerThread(name, priority) { | |
protected void onLooperPrepared() { | |
semaphore.release(); | |
} | |
}; | |
handlerThread.start(); | |
semaphore.acquireUninterruptibly(); | |
return handlerThread.getLooper(); | |
} | |
public void quit() { | |
getLooper().quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment