问题
If I set up my Android Manifest XML file correctly, can I handle C2DM intents (REGISTRATION and RECEIVE) in a regular service, rather than a broadcast receiver?
Clearly, the application would need to be designed for this, but I'm just curious if it's possible or if something is limiting the C2DM intents to a broadcast receiver, as every example I've read online uses a broadcast receiver, but it seems to me one could use a service as well.
回答1:
Quote from Google: An application on an Android device doesn’t need to be running to receive messages. The system will wake up the application via Intent broadcast when the the message arrives, as long as the application is set up with the proper broadcast receiver and permissions.
So, no. The only way to receive the messages is within a broadcast receiver. This is no big deal. Normally you receive the message and then you call a service. Google even provides you with an standard implementation. The Google IO session Android + App Engine: A Developer’s Dream Combination uses a wizard (see below for the installation) to generate the code.
Search for C2DMBaseReceiver
, C2DMBroadcastReceiver
and C2DMessaging
for example in the ChromeToPhone code: all you have to do with these 3 classes is to provide a class C2DMReceiver
which inherits from C2DMBaseReceiver
and set up the manifest.
Update
Google bloggt about Client Login key expiration. Therefore I expected that the plugin gets updated to include source which deals with that. Compared to the session video above some things changed. First you have to install the android wizard extra (it is not part of the Google plugin):
Then as described in the video you have to use this wizard:
In the generated project you can find
c2dm.jar
and c2dm-sources.jar
. These files are spreaded all over the internet, but don't have a home location to download the latest version. So you have to generate them via the wizard. The source doesn't include a version comment and doesn't deal with the key expiration issues.
回答2:
No, but you can receive the broadcast in a broadcast receiver and then start a service from that.
来源:https://stackoverflow.com/questions/8024666/handle-google-c2dm-intents-in-a-service