是否有任何理由继续使用 IntentService 来处理 GCM 消息? [英] Is there any reason to continue using IntentService for handling GCM messages?

查看:28
本文介绍了是否有任何理由继续使用 IntentService 来处理 GCM 消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,最近 Google 更改了他们的 GCM 文档,并且他们声称不再需要 IntentService 来处理到达的 GCM 消息.所有的处理都可以在BroadcastReceiver中完成.

As you know, recently Google changed their GCM documentation, and they claim that an IntentService is no longer required for handling arriving GCM messages. All the handling can be done in the BroadcastReceiver.

在尝试确定是否有任何充分理由继续使用 IntentService 时,我遇到了这个 引用:

When trying to figure out if there is any good reason to continue using the IntentService, I came across this quote:

WakefulBroadcastReceiver 将处理 GCM 消息的工作传递给的服务(通常是 IntentService),同时确保设备不会在此过程中返回休眠状态.包括 IntentService 是可选的——您可以选择在常规 BroadcastReceiver 中处理您的消息,但实际上,大多数应用将使用 IntentService.

A Service (typically an IntentService) to which the WakefulBroadcastReceiver passes off the work of handling the GCM message, while ensuring that the device does not go back to sleep in the process. Including an IntentService is optional—you could choose to process your messages in a regular BroadcastReceiver instead, but realistically, most apps will use a IntentService.

为什么大多数应用会使用 IntentService?是否存在直接在 BroadcastReceiver 中处理 GCM 消息不起作用的情况?

Why would most apps use an IntentService? Are there any scenarios in which handling the GCM message directly in the BroadcastReceiver won't work?

推荐答案

为什么大多数应用会使用 IntentService?

Why would most apps use an IntentService?

因为很可能您对消息所做的任何响应都需要超过 1-2 毫秒,这意味着您希望从主应用程序线程中完成这项工作.响应广播的一种常见模式是将工作委托给 IntentService.

Because most likely whatever you are doing in response to the message will take more than 1-2ms, which means that you want to get that work off the main application thread. A common pattern for doing that in response to a broadcast is to delegate the work to an IntentService.

因此,如果您响应 GCM 消息的工作涉及:

So, if your work in response to the GCM message involves:

  • 磁盘输入/输出
  • 进一步的网络 I/O(例如,从您的 Web 服务中检索其他数据)
  • 大量计算(例如图像处理)

您可能希望使用 IntentService 来执行该工作.

you will likely want to use an IntentService to perform that work.

这篇关于是否有任何理由继续使用 IntentService 来处理 GCM 消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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