1.创建优先级低的主线程任务,它只会在主线程空闲时才会执行。
Looper.myQueue().addIdleHandler(newMessageQueue.IdleHandler() {
@Override
public booleanqueueIdle() {
// do task
return false; //only once
}
});
2.如何快速创建一个主,子线程handler?
//主线程handler
android.os.Handler mainHandler =newandroid.os.Handler(Looper.getMainLooper());
//子线程handler
HandlerThread handlerThread =newHandlerThread("");
handlerThread.start();
android.os.Handler backgroundHandler =newandroid.os.Handler(handlerThread.getLooper());
3.如何快速判断当前是否运行在主线程?
Looper.getMainLooper().getThread() == Thread.currentThread();