我有一个工作正常的C2DM应用程序。
创建新的C2DM应用程序时,我重用了相同的程序包名称。
它起作用,除了现在在应用程序未运行时不会调用BroadcastReceiver之外。也就是说,如果我运行该应用程序并向其发送C2DM消息,则所有操作均有效。但是在强行退出后,不再调用BroadcastReceiver。
我查看了很多示例,并将旧 list 中的所有内容与新 list 中的所有内容进行了比较。要特别注意类别, Intent 服务等中使用的程序包名称。
问题:是否存在常见的C2DM编码/配置错误,导致在强制退出应用程序后无法调用BroadcastReceiver?
在强制退出应用程序后发送C2DM消息时,我确实收到了此日志消息:
01-11 00:54:43.580:WARN/GTalkService(286):[DataMsgMgr]广播 Intent 回调:result = CANCELLED forIntent {act = com.google.android.c2dm.intent.RECEIVE cat = [com.aawwpcd.pcd3] (有其他功能)}
强制退出应用程序后,对于发送给设备的每条C2DM消息,我都会获得其中之一。
似乎有 Intent ,但是没有将其传递给我的BroadcastReceiver。
编辑:
以下是 list 和BroadcastReceiver中的相关内容:
BroadcastReciever
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) {
...
}
}
list
<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接收。
最佳答案
看一下
https://stackoverflow.com/a/7108611
并在Android 3.1发行说明http://developer.android.com/about/versions/android-3.1.html#launchcontrols
从 3.1 开始,
默认情况下,强制停止的应用程序不再由 C2DM 重新启动。
有一个新标志用于在强制关闭后请求重新启动。