【发布时间】:2014-01-15 07:49:45
【问题描述】:
我创建了一个应用程序,它从我的服务器接收 GCM 消息。我试图实现为 google 的示例 (http://developer.android.com/google/gcm/client.html#sample-receive)。除非出现问题,否则它可以正常工作。也就是说,如果我在内存中杀死了我的应用程序,我的广播接收器将永远不会收到 GCM 消息。
我尝试获取设备日志,发现此问题与 GTalkService 有关:
01-15 14:19:40.509: W/GTalkService(1300): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.paktor (has extras) }
01-15 14:19:40.750: W/GTalkService(1300): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.paktor (has extras) }
01-15 14:19:41.090: W/GTalkService(1300): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.paktor (has extras) }
我的接收器在 manifest.xml 中:
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.paktor" />
</intent-filter>
</receiver>
更新: 我的广播:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(this, "Receive GCM message");
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
和权限:
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
我不知道为什么。每件事看起来都很正常。请帮助我了解如何在我的应用程序被杀死时捕获 GCM 消息。
【问题讨论】:
-
我认为你需要在 android 中看到 WakeLocker。 developer.android.com/reference/android/os/…
-
我加了android.permission.WAKE_LOCK没问题
标签: android broadcastreceiver google-cloud-messaging google-talk