服务和广播接收器之间的差异 [英] difference between services and broadcast receivers

查看:106
本文介绍了服务和广播接收器之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时试图理解服务和广播接收器之间的区别是什么,
在我看来,他们可以做同样的事情。

im trying to understand what the difference between a service and a broadcast receiver is, as i see it they can do the same thing.

例如我有一个应用程序:程序App1
提供了一个名为ToastHelloWorld服务,这只是创建了一个面包和stopSelf()。
我使用的是意图过滤器与动作的名称暴露在其他应用程序:com.test.HelloToast

For example i have an application : App1 that provide a service called ToastHelloWorld which just creates a Toast and stopSelf(). I expose it to other applications using an intent filter with the action name: "com.test.HelloToast"

现在我有其他应用程序:App2的
我想隐式使用与行动com.test.HelloToast的服务,所以我打电话
startService(新意图(com.test.HelloToast));

Now i have another application : App2 i want to implicit use a service with the action "com.test.HelloToast" so i call startService(new Intent("com.test.HelloToast"));

和它的作品。

我为什么会用广播接收器时,我可以做的一切服务,并且不具有的5秒执行限制的限制呢?

Why would i use broadcast receivers when i can do everything with services and dont have the restriction of a 5sec execution limit?.

我知道大多数系统事件通过广播'暴露,但想不出他们只是藏汉被发布为服务意图?

I know most "system events" is exposed via broadcasts' but couldnt they just aswell be published as Service Intents ?

推荐答案

广播意图通常被传递到全部为意向登记BroadcastReceivers。 (这里是有序的意图异常,其中一个接收器可以中止低优先级接收交货)开始或绑定服务只发送到有一个配套服务实例意图。

Broadcast intents are usually delivered to all BroadcastReceivers registered for that intent. (There is an exception with ordered intents where a receiver can abort the delivery for lower priority receivers) Intents that start or bind services are only sent to one matching Service instance.

有些广播意图是粘性的。这意味着他们可能在过去已发送,当你的应用程序注册接收器将交付。

Some broadcast intents are sticky. That means they might have been sent in the past and will be delivered when your app registers the receiver.

这篇关于服务和广播接收器之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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