Android 广播接收器与服务 [英] Android Broadcast Receiver vs Service

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

问题描述

我正在尝试阐明 android 中广播接收器和服务之间的区别.

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

我知道 Activity 可以通过调用 startService 来启动一个服务.

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

广播接收器可以在代码或清单中注册,并且可以使用 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 旨在在一段时间内在后台执行操作,而不管用户在前台做什么(用户可以在活动之间切换).一个很好的例子是音乐播放器服务 - 用户开始通过音乐播放器应用播放音乐,但当他们退出应用时,音乐继续播放.

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 广播一个意图时,它将被发送到所有具有匹配意图过滤器的接收器.但是,需要注意的是,在 API26+ 中,在清单中注册的大多数接收器在这种情况下不再被调用,请参阅 有关详细信息的 Google 文档.

When an intent is broadcast via sendBroadcast, it will be sent to all receivers that have matching intent filters. However, it is important to note that in API26+ most receivers registered in the manifest are no longer invoked in such situations, see the Google docs for more information.

示例 1:假设您要公开一个函数(可从任何想要使用它的应用程序中获得),该函数要求网站计算与 Kevin Bacon 的分离度.

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:

创建一个所有用户都编译到他们的应用程序中的库项目.

  • 现在您的代码有多个副本,而且它们可能都是不同的版本.
  • 您无法批处理或缓存请求,因为每个请求都是独立处理的.

创建一个广播接收器来处理每个请求.

  • 您的应用程序注册了一个广播接收器以接受询问培根问题的 Intent
  • 每个应用程序都会发送一个 Intent 来询问问题.
  • 广播接收器接受 Intent 和
    • 将请求传递给服务以进行处理,该服务将带有结果的 Intent 发送给请求者
    • 向服务器发送请求,服务器将在完成后使用 Google Cloud Messaging 进行响应

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

    • 您的应用程序创建一个服务来处理请求,并通过 Binder 或使用 AIDL 公开 API
    • API 可以是同步的(直接调用和返回)或异步的(允许监听器注册并在结果准备好时调用监听器).如果预计处理速度非常快,您应该只选择同步;服务器调用应该更频繁地异步处理
    • API 是方法调用"——一种更友好的公开功能的方式

    示例 2:您想要执行一些数据分析以在数据中找到一些模式

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

    后台线程如果所有处理都应该在用户处于同一个应用程序和同一个 Activity 上时发生,那么后台线程(或管理后台线程的 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 会是更好的方法

    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

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

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