GCM Android客户端没有触发WakefulService?

时间:2014-04-24 14:06:21

标签: java android android-intent push-notification google-cloud-messaging

我确实按照在Android应用程序中实现GCM的示例,应用程序正在使用以下代码接收消息:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp;
        comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

问题是GcmIntentService(公共类GcmIntentService扩展IntentService )永远不会被运行?我是否遗漏了某些内容,或者它是否应该触发 onHandleIntent 方法?

如果有任何相关的Manifest文件,请提前感谢:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.diversivi.test"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="19"/>
    <application
            android:label="@string/app_name"
            android:icon="@drawable/ic_launcher">
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <activity
                android:name=".DemoActivity"
                android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver
                android:name=".GcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.diversivi.test" />
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="com.diversivi.test.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.diversivi.test.permission.C2D_MESSAGE" />
</manifest>

1 个答案:

答案 0 :(得分:1)

我没有在清单中看到您的GcmIntentService服务已声明。这可能是问题所在。

另外,以下一行

comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());

假设GcmIntentService类位于您应用的主程序包中(即其全名为com.diversivi.test.GcmIntentService)。如果不是这样,广播接收器就找不到服务等级。