Skip to content

Instantly share code, notes, and snippets.

@yimun
Created October 30, 2017 07:21
Show Gist options
  • Select an option

  • Save yimun/0307f892fd9146b666b3f2bd862cef47 to your computer and use it in GitHub Desktop.

Select an option

Save yimun/0307f892fd9146b666b3f2bd862cef47 to your computer and use it in GitHub Desktop.
post runnable when thread idle
private void postThreadIdleRunnable(Looper looper, final Runnable runnable) {
try {
Field field = Looper.class.getDeclaredField("mQueue");
field.setAccessible(true);
MessageQueue queue = (MessageQueue) field.get(looper);
queue.addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
runnable.run();
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment