安卓子執行緒裡更新ui資料

2021-10-01 19:00:38 字數 879 閱讀 4316

安卓的ui資料的內容,只能在主線程裡更新資料。

如果在子執行緒裡更新資料,就會報錯。

void checkthread()

}安卓10.0在viewrootimpl裡面呼叫流程說明

1、handler裡面dispatchmessage

2、transactionexecutor裡面的execute

3、transactionexecutor裡面的executelifecyclestate

4、resumeactivityitem方法execute

5、activitythread方法handleresumeactivity

6、windowmanagerimpl方法addview

7、windowmanagerglobal方法addview

8、viewrootimp方法setview

9、viwerootimp方法requestlayout

10、viewrootimp方法checkthread

在每次的執行繪製前,會呼叫檢測是否是當前的執行緒

handler.postdelayed(new runnable() 

}).start();

}},5000);

上述的方法會在子執行緒裡更新ui頁面,導致丟擲異常

呼叫流程

textview settext

textview checkforrelayout

requrestlayout constraintlayout

requestlayout view

requessetlayout viewrootimp

checkthread viewrootimp檢查當前的布局是否合適,否則會提示錯誤

安卓更新UI方法

更新ui的方法有很多 1.通過靜態方法更新 只對資料有更新的情況下可以使用這種方式,通過外部呼叫靜態方法更新ui內容 2.通過廣播更新 在更新的地方傳送廣播,intent intent new intent intent.setaction updateui activity.sendbroadca...

子執行緒更新UI

一般在winform c s程式中經常會在子執行緒中更新控制項的情況,桌面程式ui執行緒是主線程,當試圖從子執行緒直接修改控制項屬性時會出現 從不是建立控制項的執行緒訪問它 的異常提示。跨執行緒更新ui控制項的常用方法有兩種 1.使用控制項自身的invoke begininvoke方法 2.使用sy...

安卓ui執行緒優先順序

b own.settag tag installing updatedownloadbtnstate b own,game,downloadstate.finished 更新按鈕狀態 此方法包含按鈕文字和顏色 modmgr.getgamedownloadmgr installapk context,...