主线程上的 View.postDelayed() 和 Handler.postDelayed() 有什么区别?

What is the difference between View.postDelayed() and Handler.postDelayed() on the main thread?

根据Handler.postDelayed(Runnable r, long delayMillis)的文档:

Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.

另一方面View.postDelayed(Runnable action, long delayMillis):

Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the user interface thread.

我想知道从主线程调用它们时两者之间是否存在差异,特别是在 activity 被销毁时是否存在差异?

我读过这个 article 关于当我使用内部 class 处理程序时我可能会如何泄漏 Activity 我想知道使用 View.postDelayed() 是否会导致同样的问题。

例如,foo() 可能会导致问题,或者 activity 的破坏会解决 Runnable 匿名 class 持有对 [=30 的引用这一事实=]?

public class MyActiviy extends Activity {
    private void foo(View v) {
        v.postDelayed(new Runnable() {
            public void run() {
                // some delayed work
            }
        }, 60000);
        finish();
    }
}

从源头上看,View.postDelayed() 只是在内部处理程序上使用 Handler.postDelayed(),因此没有区别。

foo()可能会泄露Activity,你应该使用View.removeCallbacks()来减少这种机会。