我在创建新的C2DM应用程序时重用了相同的包名称.
它的工作原理除外,当应用程序未运行时,不会调用broadcastReceiver.也就是说,如果我运行应用程序并向其发送C2DM消息,则一切正常.但是在强制退出后,不再调用broadcastReceiver.
我查看了很多例子,并将旧清单中的所有内容与新清单进行了比较.特别注意类别中使用的包名,意图服务等.
问题:是否存在常见的C2DM编码/配置错误导致应用程序强制退出后未调用broadcastReceiver?
强制退出应用程序后,当我发送C2DM消息时,我确实得到了这个日志:
01-11 00:54:43.580:WARN / GTalkService(286):[DataMsgMgr]广播意图回调:result = CANCELED forIntent {act = com.google.android.c2dm.intent.RECEIVE cat = [com.aawwpcd.pcd3] (有额外的)}
我强制退出应用程序后,为每个发送到设备的C2DM消息获取其中一个消息.
它似乎意图进入,但没有传递给我的broadcastReceiver.
编辑:
以下是Manifest和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) {
...
}
}
表现
<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手机上工作过,我找不到任何我正在做的事情.编写测试代码来证明这一点将是一件麻烦事,但我认为这是下一步.
编辑:
似乎与Force Quit有关.当我重新安装.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重新启动.在强制关闭后请求重新启动有一个新标志.