Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

android - C2DM Broadcast Receiver

I had a working C2DM application. I reused the same package name when creating a new C2DM application.

It works except now the BroadcastReceiver doesn't get called when the application is not running. That is to say it all works if I run the application and send it C2DM messages. But after a force quit the BroadcastReceiver is no longer called.

I have looked at a lot of examples and compared everything in my old manifest to the new one. Paying extra attention to package names used in categories, Intent Services, etc..

Question: Is there a common C2DM coding/config mistake that results in the BroadcastReceiver not being called after an app is force quit?

I do get this log cat when I send a C2DM message after force quit of my application:

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) }

I get one of those for each C2DM message I send to the device after force quitting the application.

It looks like the intent is coming in but not being passed to my BroadcastReceiver.

Edit:

Here are the relavant bits from the Manifest and 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

<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>

Edit: Could this be something new in 3.x ? As stated above my problems started with this new application - written for 3.x. What I want is for the C2DM to call the BroadcastReceiver even when the application is not running. I am not seeing that. Could it be a 3.x change? It worked this way before on 2.3.x phones and I can't find anything I am doing different. It will be a hassle to write the test code to prove this but that's next I guess.

Edit: Seems related to Force Quit. I don't have any problem when I re-install the .apk and then send the device a c2dm message; the Broadcast receiver picks it up. In this case the application was not already running when the C2DM came in yet everything worked as expected. The only problem I am having is after I force quit an application. C2DM messages after that are not picked up by the BroadcastReceiver.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at https://stackoverflow.com/a/7108611

And at Android 3.1 release notes http://developer.android.com/about/versions/android-3.1.html#launchcontrols

As of 3.1, Force stopped apps no longer get restarted by C2DM by default. There is a new flag for requesting a restart after force-close.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...