如何Android的事件监听器工作? [英] How do android's Event Listeners work?

查看:121
本文介绍了如何Android的事件监听器工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何事件从视图对象捕捉?只有一个线程在运行:UI线程(在我们还没有实现任何我们自己的线程)。假设如果我实现了一个onClickListener一个按钮,这个按钮的功能是说取消。对于通过该按钮,即可以引发事件,取消无论UI是干什么的,就必须中断。所以,是不是?难道他们的工作就像中断的?

How is an event captured from a View object? There is only one thread running : the UI thread (when we haven't implemented any of our own threads). Suppose if I have implemented an onClickListener for a button and this button's function is say "cancel". For the event to be raised by the button i.e., cancel whatever the UI is doing, it must interrupt. So is that it? Do they work like interrupts?

在开发者网站的API指南是美丽的解释,但仍然没有给出完整的画面。 http://developer.android.com/guide/topics/ui/ui -events.html

The API guides at the developer site are beautiful explanations but still don't give the complete picture. http://developer.android.com/guide/topics/ui/ui-events.html

推荐答案

在内部,Android正在运行的事件循环处理UI事件。对于一个不错的图,请参见<一个href=\"https://wiki.oulu.fi/download/attachments/29791515/Android+Event+Handling+and+App+Frameworks+Handouts.pdf?version=1&modificationDate=1363273217000\"相对=nofollow>这presentation 的第三张幻灯片。这个线程是被用来调度系统调用的UI元素:

Internally, Android is running an event loop to handle UI events. For a nice diagram, see a third slide of this presentation. This thread is being used to dispatch system calls to the UI elements:

系统不为的每个实例创建一个单独的线程
  零件。在同一进程中运行的所有部件都
  实例在UI线程,系统调用每个组件都
  从那个线程调度。

The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread.

(来源:进程和线程

有一个看一个 Android应用程序框架影片从里面谷歌I / O 2008年它的事件循环(包括活套和消息队列)的很好的解释。有趣的东西开始在大约26米到视频。

Have a look at a Inside the Android Application Framework video from Google I/O 2008. It has the nice explanation of the event loop (consisting of Looper and Message Queue). The interesting stuff starts at around 26m into the video.

的<一个href=\"http://developer.android.com/reference/android/view/View.OnClickListener.html#onClick%28android.view.View%29\"相对=nofollow>的onClick()方法将在同一个线程调用从原来的<一个href=\"http://developer.android.com/reference/android/view/View.html#setOnClickListener%28android.view.View.OnClickListener%29\"相对=nofollow> setOnClickListener()被调用。如果这是一个主/ UI线程,那么你应该在听者执行长时间运行的任务各不相同 - 它们会阻塞UI线程,并会导致应用无响应。使用解决方案,如 AsyncTask的来代替。

The onClick() method will be called from the same thread from which the original setOnClickListener() was called. If it was a main/UI thread, then you should be vary of performing long-running tasks in the listener - they will block the UI thread and can cause the app to be non-responsive. Use solutions like AsyncTask instead.

请看看的详细教程这个博客帖子

这篇关于如何Android的事件监听器工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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