Android View.post(Runnable )
2013-05-08 13:46
190 查看
Runnable 并不一定是新开一个线程,比如下面的调用方法就是运行在UI主线程中的:
官方对这个方法的解释如下,注意其中的:“The runnable will be run on the user interface thread. ”
boolean android.view.View .post(Runnable action)
Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
Parameters:
action The Runnable that will be executed.
Returns:
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.
我们可以通过调用handler的post方法,把Runnable对象(一般是Runnable的子类)传过去;handler会在looper中调用这个Runnable的Run方法执行。
Runnable是一个接口,不是一个线程,一般线程会实现Runnable。所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应线程的。
具体来说,这个函数的工作原理如下:
View.post(Runnable)方法。在post(Runnable action)方法里,View获得当前线程(即UI线程)的Handler,然后将action对象post到Handler里。在Handler里,它将传递过来的action对象包装成一个Message(Message的callback为action),然后将其投入UI线程的消息循环中。在Handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法。而此时,已经路由到UI线程里,因此,我们可以毫无顾虑的来更新UI。
如下图,前面看到的代码,我们这里Message的callback为一个Runnable的匿名内部类
这种情况下,由于不是在新的线程中使用,所以千万别做复杂的计算逻辑。
Handler mHandler=new Handler(); mHandler.post(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub } });
官方对这个方法的解释如下,注意其中的:“The runnable will be run on the user interface thread. ”
boolean android.view.View .post(Runnable action)
Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.
Parameters:
action The Runnable that will be executed.
Returns:
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.
我们可以通过调用handler的post方法,把Runnable对象(一般是Runnable的子类)传过去;handler会在looper中调用这个Runnable的Run方法执行。
Runnable是一个接口,不是一个线程,一般线程会实现Runnable。所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应线程的。
具体来说,这个函数的工作原理如下:
View.post(Runnable)方法。在post(Runnable action)方法里,View获得当前线程(即UI线程)的Handler,然后将action对象post到Handler里。在Handler里,它将传递过来的action对象包装成一个Message(Message的callback为action),然后将其投入UI线程的消息循环中。在Handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法。而此时,已经路由到UI线程里,因此,我们可以毫无顾虑的来更新UI。
如下图,前面看到的代码,我们这里Message的callback为一个Runnable的匿名内部类
这种情况下,由于不是在新的线程中使用,所以千万别做复杂的计算逻辑。
相关文章推荐
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android:UI更新方法二:View.postInvalidate+Thread+Runnable
- Android View.post(Runnable )
- android_View.post(Runnable)在onCreate获取控件宽高分析
- 通过View.post()获取View的宽高引发的两个问题:1post的Runnable何时被执行,2为何View需要layout两次;以及发现Android的一个小bug
- Android从源码分析handler.post(runnable),view.post(runnable),runOnUiThread(runnable)执行时机
- Android View.post(Runnable )
- android View post(Runnable runnable ) 新线程
- 通过View.post()获取View的宽高引发的两个问题:1post的Runnable何时被执行,2为何View需要layout两次;以及发现Android的一个小bug
- Android View.post(Runnable )的作用
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android 自定义View的post(Runnable)方法非100%执行的原因和处理方法解析
- Android View.post(Runnable )
- 关于Android View.post(Runnable ) 的使用
- Android View.post(Runnable )
- Android 自定义View的post(Runnable)方法非100%执行引起的问题定位
- 转:Android View.post(Runnable )
- Android View.post(Runnable )