5

我正在尝试从谷歌的 Android 开发者网站完成谷歌 GCM的入门步骤。

当我的设备尝试注册时,它失败了,原因如下:

Activity 已从最初在此处注册的 com.google.android.gcm.GCMBroadcastReceiver 泄露了 IntentReceiver。您是否错过了对 unregisterReceiver 的呼叫?

这是代码:

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
    GCMRegistrar.register(this, senderId); // <-- It fails here
} else {
    Log.v("GCM", "Already registered");
}

任何想法?

我究竟做错了什么?

更新

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.sample.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:name=".sample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:theme="@android:style/Theme.NoTitleBar" android:name=".Main" android:screenOrientation="portrait" />
        <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.sample" />
          </intent-filter>
        </receiver>               
        <service android:name="com.sample.GCMIntentService" />
        <uses-library android:name="com.google.android.maps" />
    </application>

</manifest>
4

4 回答 4

12

最后它起作用了。

我需要做的就是GCMRegistrar.onDestroy(this)onDestroy()我正在调用的同一上下文中调用方法GCMRegistrar.register()

在这里找到:Google Cloud Messaging 中泄露的 IntentReceiver

谢谢你的支持 :)

于 2012-07-03T09:53:32.170 回答
2

您应该使用应用程序上下文,而不是像下面这样的活动上下文:

public String registerPushAccount()
{
    GCMRegistrar.checkDevice(getApplicationContext());
    GCMRegistrar.checkManifest(getApplicationContext());
    if (GCMRegistrar.isRegistered(getApplicationContext()))
    {
        Log.d("info", GCMRegistrar.getRegistrationId(getApplicationContext()));
    }
    String regId = GCMRegistrar.getRegistrationId(getApplicationContext());
    if (regId.equals(""))
    {
        regId = GCMRegistrar.getRegistrationId(getApplicationContext());
    }
    else
    {
        Log.d("info", "already registered as" + regId);
    }
    Log.d("info", regId);
    return regId;
}

这解决了我的问题。:)

于 2013-04-24T12:04:24.813 回答
0

我已经让它与这个清单一起工作:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="PACKAGENAME"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <permission android:name="PACKAGENAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="PACKAGENAME.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="android.permission.WAKE_LOCK" android:protectionLevel="signatureOrSystem"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service android:name="PACKAGENAME.GCMIntentService" />
        <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="PACKAGENAME" />
  </intent-filter>
</receiver> 
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

还要向您的类添加一个空构造函数,该构造函数从 GCMBaseIntentService 扩展。在我的例子中命名为:GCMIntentService。

于 2012-07-03T09:02:09.227 回答
0

当我没有正确设置权限时,这发生在我身上。我通过在清单中添加来解决它:

<permission android:name="PACKAGE.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="PACKAGE.permission.C2D_MESSAGE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
于 2012-07-01T10:52:56.340 回答