4

我正在尝试从库项目中注册到 GCM,GCMIntentService 在库项目中定义。

这是我的注册码(在图书馆项目内):

public static void init(Context context, String id){
    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    GCMRegistrar.register(context, id);
}

但是没有调用 GCMIntentService 的回调,只有当我将库项目作为独立的 android 项目运行时才会调用它们。

这是我的测试项目清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.example.pushtest.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <!-- GCM requires a Google account. -->
    <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" />
    <uses-permission android:name="com.example.pushtest.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".PushTestApp" >
        <activity
            android:name="com.example.pushtest.MainActivity"
            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="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.example.pushtest" />
            </intent-filter>
        </receiver>
    </application>
    //push library definition
    <service android:name="com.test.pushlibrary.GCMIntentService" />

</manifest>
4

1 回答 1

14

这是你必须做的:

编写一个覆盖 GCMBroadcastReceiver 的类(在您的库项目中):

package com.test.pushlibrary;

import android.content.Context;

import com.google.android.gcm.GCMBroadcastReceiver;


public class PushLibraryBroadcastReceiver extends GCMBroadcastReceiver
{
    /**
     * Gets the class name of the intent service that will handle GCM messages.
     */
    @Override
    protected String getGCMIntentServiceClassName(Context context) {
        return "com.test.pushlibrary.GCMIntentService";
    }
}

更改清单以引用新的接收器:

    <receiver
        android:name="com.test.pushlibrary.PushLibraryBroadcastReceiver"
        ...

您的原始代码不起作用的原因是默认实现getGCMIntentServiceClassName假定意图服务类位于应用程序的包中,因此它期望com.example.pushtest.GCMIntentService而不是com.test.pushlibrary.GCMIntentService.

于 2013-06-20T14:23:04.283 回答