欲了解更多有关Android的UI线程的事件队列 [英] Want to understand more about Android UI Thread's Event Queue

查看:155
本文介绍了欲了解更多有关Android的UI线程的事件队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有网站和堆栈溢出有引用到UI线程的事件队列。例如 runOnUiThread()的将发布一个动作到UI线程的事件队列。但我一直没能找到这个队列的详细描述,所以可能有人请点我一个详细的一个,或者回答几个问题?

1 我明白这是一个队列,它包含了行动,但我是一个有点不清楚什么是行动。对线程本身,或者什么样的行动方法及其相关的参数调用,或指令?

2 是否所有的线程具有事件队列,或只是在UI线程?

3。我怎么能看到什么事件队列,或获取事件的计数?

4。究竟什么决定了执行队列中的一个动作是什么时候?

5。 View类有一个叫方法的 cancelPendingInputEvents()的是用来的取消任何延迟的高电平输入事件为previously发布到事件队列的。如果事件队列是一个线程的属性,这是为什么视图类的方法,或者说观点有一些不同的事件队列?

6。是消息队列和事件队列两个不同的队列? NB - 有人问这对SO <一href="http://stackoverflow.com/questions/22713303/difference-between-event-queue-and-message-queue">here并说他们是同义词,然后开始了回答者追加这似乎意味着信息是不同的,所以我不清楚最终的答案是增编。

解决方案
  1. 这是一个用的Runnable队列。在每个可运行的
  2. ;线程调用的run()。
  3. 在那个叫活套。prepare只有线程(),所以任何线程都可能有他们。这里有一个运行时异常为内螺纹无法创建处理程序,并没有所谓的活套。prepare()
  4. 您不能。东西是通过平台进行管理,并呼吁活动的回调,回调片段,调度触摸事件,运行动画,运行布局,测量和绘制。这在UI线程所有。
  5. AFAIK这是一个FIFO。但我可能是错在那一个。
  6. 查看有一个处理程序的UI线程。处理程序绑定到线程和它的MessageQueue。这就是你可以通过调用新处理器()在UI线程上创建一个新的UI线程处理器。然后通过调用张贴的东西到线程队列 handler.post(可运行)
  7. 我不相信他们是不同的。不过,如果要挖源$ C ​​$ C是肯定的。

它总是有帮助的阅读文档:

https://developer.android.com/reference/android/os/ Handler.html

https://developer.android.com/reference/android/os/ MessageQueue.html

All over the web and on Stack Overflow there are references to the UI Thread's Event Queue. For example runOnUiThread() will post an action to the UI thread's Event Queue. But I haven't been able to find a detailed description of this queue, so could someone please point me to a detailed one, or answer a few questions?

1. I get that it's a queue and that it contains "actions", but I'm a little unclear what an "action" is. Are actions method calls with their associated parameters, or instructions to the thread itself, or what?

2. Do all threads have event queues or just the UI thread?

3. How can I see what's in the Event Queue, or get a count of events?

4. What exactly determines when an action in the queue is executed?

5. The View class has a method called cancelPendingInputEvents() which is used to "Cancel any deferred high-level input events that were previously posted to the event queue." If the event queue is a property of a thread, why is this a method of the View class, or do views have some different Event Queue?

6. Are the message queue and event queue two different queues? N.B. - someone asked this on SO here and the answerer started by saying they were synonymous and then appended an addendum which seemed to imply messages were different so I'm unclear what the final answer was.

解决方案

  1. it's a queue with Runnables. The thread calls run(); on each of the runnables.
  2. only threads that called Looper.prepare(), so any thread can potentially have them. There's an Runtime Exception for that "Can't create handler inside thread that has not called Looper.prepare()"
  3. You can't. Stuff is managed by the platform and calls Activity callbacks, Fragment callbacks, dispatch touch events, run animations, run layout, measure and draw. All this in the UI thread.
  4. AFAIK it's a FIFO. But I might be wrong on that one.
  5. Views have a Handler to the UI thread. Handlers are bound to the thread and it's MessageQueue. That's how you can create a new UI thread handler by calling new Handler() on the UI thread. And then post stuff to that thread queue by calling handler.post(Runnable)
  6. I don't believe they're different. But would have to dig on source code to be sure.

It's always helpful to read the docs:

https://developer.android.com/reference/android/os/Handler.html

https://developer.android.com/reference/android/os/MessageQueue.html

这篇关于欲了解更多有关Android的UI线程的事件队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆