我有一个可用的C2DM应用程序。在创建新的C2DM应用程序时,我重复使用了相同的包名。
它工作得很好,只是当应用程序未运行时,广播接收器不会被调用。也就是说,如果我运行该应用程序并将C2DM消息发送到它,一切都正常。但在强制退出之后,广播接收器不再被调用。
我查看了许多示例,并将旧清单中的所有内容与新清单进行了比较。特别注意类别、意图服务等中使用的包名称。
问题: 有没有常见的C2DM编码/配置错误导致在应用程序被强制退出后 BroadcastReceiver 不被调用?
当我在强制退出我的应用程序之后发送C2DM消息时,我确实收到了下面这条日志:
01-11 00:54:43.580: WARN/GTalkService(286): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.aawwpcd.pcd3] (has extras) }
每次我向设备发送 C2DM 消息后,我都会收到这样的消息。
看起来意图已经进来了,但却没有传递给我的广播接收器。
编辑:
以下是来自清单文件和广播接收器的相关部分:
广播接收器
package com.aawwpcd.pcd3.c2dm;
import ...
public class C2DMBroadcastReceiver extends BroadcastReceiver {
@Override
public IBinder peekService(Context myContext, Intent service) {
return super.peekService(myContext, service);
}
public C2DMBroadcastReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
...
}
}
清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aawwpcd.pcd3"
android:versionCode="250"
android:versionName="ICSPCD3">
<uses-sdk android:minSdkVersion="13"
android:targetSdkVersion="14"/>
<permission android:name="com.aawwpcd.pcd3.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.aawwpcd.pcd3.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:name=".PCD3Application"
android:label="@string/app_name"
android:icon="@drawable/pcdlauncher"
android:theme="@android:style/Theme.Holo">
<activity android:name=".honeycombpcd3.FullScheduleActivity"
android:label="@string/app_namefull"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Only C2DM servers can send messages for the app. If permission is not
set - any other app can generate it -->
<receiver android:name=".c2dm.C2DMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.aawwpcd.pcd3"/>
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="com.aawwpcd.pcd3"/>
</intent-filter>
</receiver>
</application>
</manifest>
编辑:这可能是3.x中的新东西吗?如上所述,我的问题始于这个为3.x编写的新应用程序。我希望C2DM在应用程序未运行时调用BroadcastReceiver。但我并没有看到。这可能是3.x的变化吗?它以前在2.3.x手机上就是这样工作的,而我找不到任何不同的地方。证明这一点需要编写测试代码,但我想那是下一步了。
编辑: 似乎与强制退出有关。当我重新安装.apk,并向设备发送c2dm消息时,我没有任何问题;广播接收器捕捉到了它。在这种情况下,当C2DM进入时应用程序尚未运行,但一切都如预期的那样工作。唯一的问题是在我强制退出应用程序后。此后的C2DM消息不会被BroadcastReceiver捕获。
<receiver ... />
标签以及接收器类的包名。 - Jin35