android 中使用View的訊息佇列api更新資料

來源:互聯網
上載者:User

標籤:android   http   io   os   ar   使用   java   sp   資料   

基本上只要繼承自View的控制項,都具有訊息佇列或者handler的一些處理方法,下面是一些handler方法以及被View封裝了的方法,其底層用的基本都是handler的api。

我麼開一下postDelay的定義

android.view.View

 public boolean postDelayed(Runnable action, long delayMillis) {        final AttachInfo attachInfo = mAttachInfo;        if (attachInfo != null) {            return attachInfo.mHandler.postDelayed(action, delayMillis);        }        // Assume that post will succeed later        ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);        return true;    }


不僅如此,Activity中也有一個方法,這個方法發送訊息到UI線程,runOnUIThread(Runnable action);

先來看看他的視線

java.app.Activity

 public final void runOnUiThread(Runnable action) {        if (Thread.currentThread() != mUiThread) {            mHandler.post(action);        } else {            action.run();        }    }

很顯然,當方法運行在UI線程上時立即執行,否則發送訊息到UI線程之後再執行。

runOnUIThread又為我們省卻了建立Handler的非同步,比如在多線程中,在子線程中再也不用使用handler發送訊息了,完全可以在子線程中調用這個方法來“更新”UI了(注意哦,這裡的實際上會發送訊息和執行程式碼片段到UI,並不是在子線程上更新)





android 中使用View的訊息佇列api更新資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.