简体   繁体   English

Google Cloud Messaging:BroadcastReceiver还是Service?

[英]Google Cloud Messaging: BroadcastReceiver or Service?

What is the generally preferred method of using GCM in its current state? 在当前状态下使用GCM的一般首选方法是什么?

The documentation only talks about using it with a BroadcastReceiver and only mentions Services in one sentence without further explanation. 文档仅讨论将其与BroadcastReceiver一起使用,仅用一句话提到Services ,而没有进一步的说明。

In my application, I need to be able to react to an unknown number of successive GCM messages and queue them so I can process them one by one. 在我的应用程序中,我需要能够对未知数量的连续GCM消息做出反应并将它们排队,以便可以一个接一个地处理它们。 This processing needs to be done in order the messages are received in. 需要进行此处理才能接收消息。

This cannot be done with a BroadcastReceiver , as for every broadcast received, a new instance of my receiver class is created (this was the method I tried first as per the getting started guide). 这不能使用BroadcastReceiver来完成,因为对于每个接收到的BroadcastReceiver ,都会创建我的接收器类的新实例(根据入门指南,这是我首先尝试的方法)。 Can it be done with a service or, more precisely, is the instance of my service kept between messages received? 可以通过服务来完成,或者更确切地说,可以在收到的消息之间保留我的服务实例吗?

If so, when and how does this service need to be started, added to the manifest, etc.? 如果是这样,该服务何时,如何以及如何启动,添加到清单等?

I do not need to interact with my main application/UI. 我不需要与主应用程序/ UI进行交互。 The service can do its business on its own. 该服务可以自行开展业务。

GCM message comes as broadcast so you must use BroadcastReceiver . GCM消息是作为广播发送的,因此您必须使用BroadcastReceiver If you need to queue them for any reason. 如果出于任何原因需要排队。 just make your BroadcastReceiver hand the message to IntentService or anything else you find suitable for your task. 只需使您的BroadcastReceiver将消息传递给IntentService或您认为适合于任务的任何其他东西即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM