UIthread UI 线程


The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components(Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those applications are performed in this thread.

For instance, lets say your application is a single Activity class. Then all of the lifecycle methods and most of your event handling code is run in this UIThread. These are methods like OnCreate, OnPause, OnDestroy, OnClick, etc. Additionally, this is where all of the updates to the UI are made. Anything that causes the UI to be updated or changed HAS to happen on the UI thread.

For more info on your applications Processes and Threads click here.

When you explicitly spawn a new thread to do work in the background, this code is not is not run on the UIThread. So what happens if the this background thread needs to do something that changes the UI? This is what therunOnUiThreadis for. Actually you're supposed to use a Handler (see the link below for more info on this); it provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the RunOnUiThread method.

For more info on spawning worker threads and updating the UI from them click here

I personally only use the RunOnUiThread method in my Instrumentation Tests. Since the test code does not execute in the UIThread, you need to use this method to run code that modifies the UI. So, I use it to inject click and key events into my application. I can then check the state of the application to make sure the correct things happened

For more info on testing and running code on the UIThread click here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值