Android的广播接收器VS服务接收器、Android、VS

2023-09-05 05:28:52 作者:可望不可及

我想澄清一个广播接收器,服务于一体的Andr​​oid之间的差异。

I am trying to clarify the difference between a Broadcast Receiver and Service in android.

据我所知,一个活动可以通过调用 startService ,意图启动服务。

I understand that an activity can start a service by calling startService with an intent.

一个广播接收器可以注册在code或清单,并且可以调用 sendBroadcast

A broadcast receiver can be registered in code or the manifest and can be called with sendBroadcast.

当你使用一个相较于其他?

据我所知,多个广播接收机可以侦听相同的目的,这是不是与服务的情况。

I understand that multiple broadcast receiver's can be listening for the same intent and this is NOT the case with a service.

推荐答案

服务是为了在后台执行的操作一段时间,而不管用户在做在前台(用户可以活动之间进行切换)。一个很好的例子将是一个音乐播放器服务 - 用户开始通过音乐播放器应用程序,播放音乐,但是,当他们离开该应用程序的音乐继续播放

Services are meant to perform an action in the background for some period of time, regardless of what the user is doing in foreground (the user could be switching between activities). A good example would be a music player service - the user starts playing music through a music player app but when they exit the app the music keeps playing.

服务,也可用于提供/管理共同访问多个应用程序的资源。这通常用于系统资源,诸如传感器

Services are also useful to provide/manage common access to a resource across multiple applications. This is often used for system resources, such as sensors.

广播接收器是为了一个目的响应(通常是由一个服务或系统事件发送),做一些事情,并完成。一个例子这里可能是用户触摸具有NFC功能的手机标签,系统为它创建一个意图,并注册接收处理它更改某些设置(改变音量,开启蓝牙,等等)。

Broadcast receivers are meant to respond to an intent (usually one sent by a service or a system event), do something, and be done. An example here might be the user touches an NFC-enabled phone to a tag, the system creates an intent for it, and a registered receiver handles it to change some settings (change volume, turn on bluetooth, etc).

当一个意图通过sendBroadcast广播,它将被送往的所有的接收器有匹配意图过滤器。

When an intent is broadcast via sendBroadcast, it will be sent to all receivers that have matching intent filters.

例1:假设你要公开一个功能(可从想要使用它的任何应用程序),要求网站来计算凯文·贝肯分离度

Example 1: Suppose you want to expose a function (to be available from any application that wants to use it) that asks a website to calculate degrees of separation from Kevin Bacon.

请注意,这个例子是做一些与回报,而不是执行一个长期运行的后台操作。

Note that this example is "do something and return", as opposed to perform a long-running background operation.

您可以通过多种方式实现这一点:

You could implement this in several ways:

创建库项目的所有用户都编译为他们的应用程序。

现在有你的code的多个副本,它们都可能是不同的版本。 您不能批处理或缓存请求为每个请求单独处理。

创建广播接收机来处理每个请求。

您的应用程序注册的广播接收器接受一个I​​ntent问培根问题 在每个应用程序发送一个意向要问的问题。 广播接收机接收的意图,要么 将请求传递到服务做加工,它发送一个Intent给请求者,结果 发送一个请求,这将当它完成响应使用谷歌云消息传递的服务器 Your application registers a broadcast receiver to accept an Intent asking the Bacon question Each application sends an Intent to ask the question. The broadcast receiver accepts the Intent and either Passes the request to a service to do the processing, which sends an Intent to the requester with the result Sends a request to the server which will respond using Google Cloud Messaging when it's done

创建的服务来处理每一个请求

您的应用程序创建一个服务来处理请求,并通过活页夹或使用AIDL 公开的API 在该API可以是同步的(直接调用和返回)或异步(允许听众注册和呼叫监听器当结果准备好)。您应该只选择同步如果处理预计将非常快;服务器调用应该更经常被异步处理 API是方法调用 - 一个更友好的方式来揭露功能

例2:要执行一些数据分析,找到一些模式在数据

Example 2: You want to perform some data analysis to find some patterns in your data

后台线程如果所有的处理应该发生,而用户是在同一个应用程序,并在同一个活动,一个后台线程(或管理一个后台线程的AsyncTask的),将是一个很好的办法

Background Thread If all processing should happen while the user is in the same application and on the same Activity, a background thread (or an AsyncTask that manages a background thread) would be a good approach

服务如果你想允许用户退出应用程序,同时正在执行的处理(其结果后来通知他们),或允许他们通过多种活动,在同一个应用进展而正在执行的处理,服务将是一个更好的办法

Service If you want to allow the user to exit the application while the processing is being performed (and notify them of the results later), or allow them to progress through multiple activities in the same application while the processing is being performed, a Service would be a better approach

 
精彩推荐