Android: java.lang.IllegalStateException: No recei

2019-09-11 15:55发布

问题:

Hey guys I am trying to implement GCM to my app. However I keep getting this annoying error for some reason:

java.lang.IllegalStateException: No receiver allowed to receive com.google.android.c2dm.permission.SEND

Here is my manifest file:

    <!-- GCM -->

<!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
 <permission
    android:name="com.myapp.user.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.myapp.user.permission.C2D_MESSAGE" />
<!-- Permission to vibrate -->
 <uses-permission android:name="android.permission.VIBRATE" />

<!-- GCM -->
<application
 <activity
        android:name=".Home"
        android:configChanges="keyboard|orientation|navigation|locale"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >

        <receiver
            android:name=".GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.myapp.user" />
            </intent-filter>
        </receiver>

        <service
            android:name=".GCMIntentService"
            android:exported="true"/>

    </activity>
</application

What could be the cause of the error. I must say that I don't have a GCMBroadcastReceiver class in my project do I need one? Also I must say that all of my classes are in the same package/folder inside the project.

回答1:

The <receiver> and <service> are inside your <activity> but it should be inside <application>.

Like this:

<application>

    <activity
        android:name=".Home"
        android:configChanges="keyboard|orientation|navigation|locale"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
    </activity>

    <receiver
        android:name=".GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.myapp.user" />
        </intent-filter>
    </receiver>

    <service
        android:name=".GCMIntentService"
        android:exported="true"/>

</application>