Android UI thread / main thread
来源:互联网 发布:购物车数据库表设计 编辑:程序博客网 时间:2024/06/03 00:57
UI thread
When an application is launched, the system creates a thread called "main" for the application. The main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widgets, including drawing events. It is also the thread where your application interacts with running components of the Android UI toolkit.
Android UI principle: single-threaded model for the UI: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread.
在android中经常需要用到异步操作,Thread+Handler方式感觉繁琐,AsyncTask只能执行一次,很多需求不能满足,这时我们可以试试Activity提供的另外一种简单的方法runOnUiThread,runOnUiThread可以帮助你在线程中执行UI更新操作。
以下为代码:
- MyActivity.this. runOnUiThread(new Runnable() {
- @Override
- public void run() {
- // refresh ui 的操作代码
- }
- });
- public final void runOnUiThread(Runnable action) {
- if (Thread.currentThread() != mUiThread) {
- mHandler.post(action);
- } else {
- action.run();
- }
- }
从代码可以看到,runOnUiThread首先判断是否是UI线程,不是的话就post,如果是的话就正常运行该线程.
只要经过主线程中的Handler.post或者postDelayed处理线程runnable则都可以将其转为UI主线程.再说Handler的机制就是来处理线程与UI通讯的.
原文链接:http://blog.csdn.net/androiddeveloper_lee/article/details/6790552
- Android UI thread / main thread
- Android UI thread / main thread
- Main Thread & UI Thread
- android UI thread
- Main Thread Checker: UI API called on a background thread:
- main thread in android application
- UI Thread & Worker Thread
- Android UI Operation in Thread
- 【Android】child thread refresh UI
- 深入理解android UI Thread
- ui thread
- UI Thread
- baber main thread barber thread
- Thread Synchronization - UI Thread and Worker Thread
- main Queue 与 main Thread
- main thread starting…
- Android Handler+Thread实现更新Ui
- 08 Android 使用Thread操作UI
- 时间格式随系统格式
- Android [LED]如何配置LCD背光和LED,调试方法 M
- 天然脑黄金的八种食物
- [easyui]复合表头隐藏列时表头出现错位的修复
- IOS多线程及队列的使用
- Android UI thread / main thread
- MySQL int(M)了解
- 使用dtree生成的树结构,ajax动态加载子节点后,如何绑定右键菜单续
- Spring3:如何配置静态文件映射?
- Android USB OTG功能如何打开及实现
- 2013-12-11 17:58:25
- 二叉树的遍历(前序、中序、后序、层次)
- Akka 对比 Storm
- EJB3: JBOSS 企业版、JBOSS服务器构架、EJB2.0 HelloWrold实例