Android上的view.post()和view.getHandler().post()有什么区别?

Android上的view.post()和view.getHandler().post()有什么区别?,android,android-view,android-handler,Android,Android View,Android Handler,关于view.post()的文档说明: 使Runnable添加到消息队列中。可运行的 将在用户界面线程上运行 view.getHandler()返回以下内容: 与运行视图的线程相关联的处理程序 我知道视图可以在后台线程中创建,但它们总是在UI线程上运行。这意味着view.getHandler()应始终返回与UI线程关联的处理程序-本质上使view.getHandler().post()和view.post()发布到同一消息队列 为什么我要使用view.getHandler().post()?除了

关于
view.post()的文档说明:

使Runnable添加到消息队列中。可运行的 将在用户界面线程上运行

view.getHandler()
返回以下内容:

与运行视图的线程相关联的处理程序

我知道视图可以在后台线程中创建,但它们总是在UI线程上运行。这意味着view.getHandler()应始终返回与UI线程关联的处理程序-本质上使view.getHandler().post()和view.post()发布到同一消息队列


为什么我要使用view.getHandler().post()?

除了
getHandler(),没有太大区别。post()
可以将您抛出一个
NullPointerException
,因为它可以返回null

/**
 * @return A handler associated with the thread running the View. This
 * handler can be used to pump events in the UI events queue.
 */
public Handler getHandler() {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler;
    }
    return null;
}
同时,它只是在相同条件下重定向,但返回布尔值

/**
 * <p>Causes the Runnable to be added to the message queue.
 * The runnable will be run on the user interface thread.</p>
 *
 * @param action The Runnable that will be executed.
 *
 * @return Returns true if the Runnable was successfully placed in to the
 *         message queue.  Returns false on failure, usually because the
 *         looper processing the message queue is exiting.
 *
 * @see #postDelayed
 * @see #removeCallbacks
 */
public boolean post(Runnable action) {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler.post(action);
    }

    // Postpone the runnable until we know on which thread it needs to run.
    // Assume that the runnable will be successfully placed after attach.
    getRunQueue().post(action);
    return true;
}
/**
*导致将Runnable添加到消息队列中。
*runnable将在用户界面线程上运行

* *@param action将要执行的可运行操作。 * *@return如果将Runnable成功放置到 *消息队列。失败时返回false,通常是因为 *正在退出处理消息队列的循环器。 * *@see#postdayed *@see#移除回调 */ 公共布尔post(可运行操作){ 最终附件fo AttachInfo=mAttachInfo; if(attachInfo!=null){ 返回附件mHandler.post(操作); } //推迟runnable,直到我们知道它需要在哪个线程上运行。 //假设在附加后将成功放置runnable。 getRunQueue().post(操作); 返回true; }
除了
getHandler(),没有太大区别。post()
可以将您抛出到
NullPointerException
,因为它可以返回null

/**
 * @return A handler associated with the thread running the View. This
 * handler can be used to pump events in the UI events queue.
 */
public Handler getHandler() {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler;
    }
    return null;
}
同时,它只是在相同条件下重定向,但返回布尔值

/**
 * <p>Causes the Runnable to be added to the message queue.
 * The runnable will be run on the user interface thread.</p>
 *
 * @param action The Runnable that will be executed.
 *
 * @return Returns true if the Runnable was successfully placed in to the
 *         message queue.  Returns false on failure, usually because the
 *         looper processing the message queue is exiting.
 *
 * @see #postDelayed
 * @see #removeCallbacks
 */
public boolean post(Runnable action) {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler.post(action);
    }

    // Postpone the runnable until we know on which thread it needs to run.
    // Assume that the runnable will be successfully placed after attach.
    getRunQueue().post(action);
    return true;
}
/**
*导致将Runnable添加到消息队列中。
*runnable将在用户界面线程上运行

* *@param action将要执行的可运行操作。 * *@return如果将Runnable成功放置到 *消息队列。失败时返回false,通常是因为 *正在退出处理消息队列的循环器。 * *@see#postdayed *@see#移除回调 */ 公共布尔post(可运行操作){ 最终附件fo AttachInfo=mAttachInfo; if(attachInfo!=null){ 返回附件mHandler.post(操作); } //推迟runnable,直到我们知道它需要在哪个线程上运行。 //假设在附加后将成功放置runnable。 getRunQueue().post(操作); 返回true; }
Android是开源的。。。Android是开源的。。。一个区别是View.post根据
getRunQueue().post(action)在线程未附加到视图的情况下将操作缓存在队列中
一个区别是View.post根据
getRunQueue().post(action)在线程未附加到视图的情况下将操作缓存在队列中