@Test public void postDelayedWithToken() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); mHandler.postDelayed(new Runnable() { @Override public void run() { latch.countDown(); } }, 300); final List<String> events = new ArrayList<>(); final Object token = new Object(); HandlerCompat.postDelayed(mHandler, new Runnable() { @Override public void run() { events.add("200"); } }, token, 200); HandlerCompat.postDelayed(mHandler, new Runnable() { @Override public void run() { events.add("100"); mHandler.removeCallbacksAndMessages(token); } }, token, 100); mHandler.post(new Runnable() { @Override public void run() { events.add("0"); } }); assertTrue(latch.await(1, SECONDS)); assertEquals(asList("0", "100"), events); }
public static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r, @Nullable Object token, long delayMillis) { if (BuildCompat.isAtLeastP()) { return handler.postDelayed(r, token, delayMillis); } Message message = Message.obtain(handler, r); message.obj = token; return handler.sendMessageDelayed(message, delayMillis); }
HandlerCompat { public static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r, @Nullable Object token, long delayMillis) { if (BuildCompat.isAtLeastP()) { return handler.postDelayed(r, token, delayMillis); } Message message = Message.obtain(handler, r); message.obj = token; return handler.sendMessageDelayed(message, delayMillis); } }
HandlerCompat { public static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r, @Nullable Object token, long delayMillis) { if (BuildCompat.isAtLeastP()) { return handler.postDelayed(r, token, delayMillis); } Message message = Message.obtain(handler, r); message.obj = token; return handler.sendMessageDelayed(message, delayMillis); } private  HandlerCompat(); }
HandlerCompat { public static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r, @Nullable Object token, long delayMillis) { if (BuildCompat.isAtLeastP()) { return handler.postDelayed(r, token, delayMillis); } Message message = Message.obtain(handler, r); message.obj = token; return handler.sendMessageDelayed(message, delayMillis); } private  HandlerCompat(); static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r,
            @Nullable Object token, long delayMillis); }
HandlerCompat { public static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r, @Nullable Object token, long delayMillis) { if (BuildCompat.isAtLeastP()) { return handler.postDelayed(r, token, delayMillis); } Message message = Message.obtain(handler, r); message.obj = token; return handler.sendMessageDelayed(message, delayMillis); } private  HandlerCompat(); static boolean postDelayed(@NonNull Handler handler, @NonNull Runnable r,
            @Nullable Object token, long delayMillis);  }