android – C2DM广播接收器

前端之家收集整理的这篇文章主要介绍了android – C2DM广播接收器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个有效的C2DM应用程序.
我在创建新的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

  1. package com.aawwpcd.pcd3.c2dm;
  2.  
  3. import ...
  4.  
  5. public class C2DMBroadcastReceiver extends BroadcastReceiver {
  6.  
  7. @Override
  8. public IBinder peekService(Context myContext,Intent service) {
  9. return super.peekService(myContext,service);
  10. }
  11.  
  12. public C2DMBroadcastReceiver() {
  13. super();
  14. }
  15.  
  16. @Override
  17. public void onReceive(Context context,Intent intent) {
  18.  
  19. ...
  20.  
  21. }
  22.  
  23. }

表现

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.aawwpcd.pcd3"
  3. android:versionCode="250"
  4. android:versionName="ICSPCD3">
  5.  
  6. <uses-sdk android:minSdkVersion="13"
  7. android:targetSdkVersion="14"/>
  8.  
  9. <permission android:name="com.aawwpcd.pcd3.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
  10. <uses-permission android:name="com.aawwpcd.pcd3.permission.C2D_MESSAGE"/>
  11. <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
  12.  
  13. <uses-permission android:name="android.permission.INTERNET"/>
  14. <uses-permission android:name="android.permission.WAKE_LOCK"/>
  15.  
  16. <application android:name=".PCD3Application"
  17. android:label="@string/app_name"
  18. android:icon="@drawable/pcdlauncher"
  19. android:theme="@android:style/Theme.Holo">
  20.  
  21. <activity android:name=".honeycombpcd3.FullScheduleActivity"
  22. android:label="@string/app_namefull"
  23. >
  24.  
  25. <intent-filter>
  26. <action android:name="android.intent.action.MAIN"/>
  27. <category android:name="android.intent.category.LAUNCHER"/>
  28. </intent-filter>
  29.  
  30. </activity>
  31.  
  32. <!-- Only C2DM servers can send messages for the app. If permission is not
  33. set - any other app can generate it -->
  34. <receiver android:name=".c2dm.C2DMBroadcastReceiver"
  35. android:permission="com.google.android.c2dm.permission.SEND">
  36.  
  37. <!-- Receive the actual message -->
  38. <intent-filter>
  39. <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
  40. <category android:name="com.aawwpcd.pcd3"/>
  41. </intent-filter>
  42.  
  43. <!-- Receive the registration id -->
  44. <intent-filter>
  45. <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
  46. <category android:name="com.aawwpcd.pcd3"/>
  47. </intent-filter>
  48.  
  49. </receiver>
  50.  
  51. </application>
  52.  
  53. </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重新启动.在强制关闭后请求重新启动有一个新标志.

猜你在找的Android相关文章