监听器和一个接收器(安卓)之间的区别是什么? [英] What is the difference between a Listener and a Receiver (Android)?

查看:205
本文介绍了监听器和一个接收器(安卓)之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我需要一个BroadcastReceiver来获得这些事件:

For instance, I need a BroadcastReceiver to get these events:

重启或关机

屏幕或关闭

电池状态(电压,插上电源,温度)

battery status (voltage, plugged in, temperature)

物理按键presses(摄像头,媒体等。)

physical button presses (camera, media, etc.)

但我需要监听器来获得这些事件:

But I need Listener to get these events:

事件监听对传感器事件(加速度,磁场,取向,接近,温度,光照水平,等等。)

EventListener for sensor events (acceleration,magnetic fields, orientation, proximity, temperature, light level, etc.)

LocationListener的用于定位事件(网络位置,GPS)

LocationListener for location events (Network location, GPS)

这似乎是两个接收机和听众都存在这样我就可以接收事件。除了显而易见的语义差别,什么是两者之间的区别是什么?有没有事情,我可以在一个做而不是其他,尤其是在问候之类的东西,我可以有多少的CPU需要和运行时的屏幕关闭?为什么有两个完全不同的结构用于接收事件的目的是什么?

It seems like both Receivers and Listeners both exist so that I can receive events. Other than the obvious semantic differences, what are the difference between the two? Are there things I can do in one but not the other, especially in regards to things like how much CPU I can take and running while the screen is off? Why are there two totally different constructs for the purposes of receiving events?

推荐答案

有这两者之间有很大差异,他们回应的东西是唯一的相似点之一。

There are so many differences between these two, them responding to something is one of the only similarities.

的区别:

  • BroadcastReceivers收到意图而一个监听器可以做基本上什么,因为它没有一个明确的目标,它只是一个命名约定。例如,搜索上开发的网站的BroadcastReceiver然后搜索的"Listener

BroadcastReceivers刚刚收到一个Intent广播是不直接的,听众得到明确要求。

BroadcastReceivers just receive an Intent Broadcast which is non-direct, Listeners get called explicitly.

A <一个href="http://developer.android.com/reference/android/content/BroadcastReceiver.html">BroadcastReceiver是它自己定义的类,因为它有一个明确的目的(接收意图),而听众可以是任何东西 - 它们通常是接口并提供他们,这样的回调可从一个类到另一个。

A BroadcastReceiver is its own defined class since it has a definite purpose (receiving Intents) whereas Listeners can be anything - they're usually an interface and they are provided so that callbacks can be made from one class to another.

BroadcastReceivers通常用于全局,全系统的事件,监听器为特定事件(即位置传感器不应该给位置更新的每一秒,除非它有一些东西,比如一个监听器发布到。兼与屏幕关闭的意图 - 这是很重要的,它影响了一切,所以必须广播给所有有兴趣的接收器)

BroadcastReceivers are usually used for global, system-wide events, Listeners for specific events (ie a Location sensor shouldn't give location updates every second unless it has something, such as a Listener to publish to. Contrasted with a screen-off intent - this is important, it affects everything, and so should be broadcast to all interested Receivers)

活动一个BroadcastReceiver拿起被的一般的非连续的事件(一杆),而监听器,这取决于他们做什么,可以用于恒更新(连续)。

Events a BroadcastReceiver picks up are usually non-continuous events (one shot), whereas Listeners, depending on what they do, can be used for constant updates (continuous).

BroadcastReceivers可以通过该系统进行实例化,如果他们在清单正在申报,听众只能动态地进行(使通过code)。

BroadcastReceivers can be instantiated by the system if they're declared in the manifest, Listeners are only dynamically made (so via code).

CPU /电源使用取决于执行的都有,特别是因为如前所述,听众可什么

CPU/Power usage depends on implementation of both, especially since as mentioned, Listeners can be anything.

有没有事情,我可以在一个做而不是其他,尤其是在问候之类的东西,我可以有多少的CPU需要和运行时的屏幕是关闭的?

Are there things I can do in one but not the other, especially in regards to things like how much CPU I can take and running while the screen is off?

BroadcastReceivers有10秒的执行时间的限制。听众因为它们不用于特定的目的,就没有这个限制了。

BroadcastReceivers have a limit of 10s execution time. Listeners since they don't serve a specific purpose, don't have this limit.

你绝对不能从一个BroadcastReceiver做了什么:

What you definitely can't do from a BroadcastReceiver:

在特定的,你可能不会显示一个对话框或绑定到从服务   在一个BroadcastReceiver。

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver.

有可能更多 - 这是我想出了

There are probably more - this is what I came up with.

这篇关于监听器和一个接收器(安卓)之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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