Unable to instantiate receiver GCMBroadcastReceiver
I’m following http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/‘s guidelines are for GCM.
Currently I am stuck in the Receive notifications section.
Sometimes when I run my code, it crashes with the following error:
7.517: E/AndroidRuntime(8601): FATAL EXCEPTION: main
7.517: E/AndroidRuntime(8601): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.gcm.GCMBroadcastReceiver: java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gcm.GCMBroadcastReceiver” on path: DexPathList[[zip file “/data/app/com.example.someactivity-1.apk”],nativeLibraryDirectories=[/data/app-lib/ com.example.someactivity-1, /vendor/lib, /system/lib]]
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2544)
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread.access$1500(ActivityThread.java:153)
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
7.517: E/AndroidRuntime(8601): at android.os.Handler.dispatchMessage(Handler.java:99)
7.517: E/AndroidRuntime(8601): at android.os.Looper.loop(Looper.java:137)
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread.main(ActivityThread.java:5295)
7.517: E/AndroidRuntime(8601): at java.lang.reflect.Method.invokeNative(Native Method)
7.517: E/AndroidRuntime(8601): at java.lang.reflect.Method.invoke(Method.java:525)
7.517: E/AndroidRuntime(8601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
7.517: E/AndroidRuntime(8601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
7.517: E/AndroidRuntime(8601): at dalvik.system.NativeStart.main(Native Method)
7.517: E/AndroidRuntime(8601): Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gcm.GCMBroadcastReceiver” on path: DexPathList[[zip file “/data/ app/com.example.someactivity-1.apk”],nativeLibraryDirectories=[/data/app-lib/com.example.someactivity-1, /vendor/lib, /system/lib]]
7.517: E/AndroidRuntime(8601): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
7.517: E/AndroidRuntime(8601): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
7.517: E/AndroidRuntime(8601): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
7.517: E/AndroidRuntime(8601): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2539)
Here is my list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.someactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.example.someactivity.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.someactivity.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<activity
android:name="com.example.someactivity.Login"
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:name="com.example.someactivity.fragmentContainer" >
</activity>
<receiver
android:name="com.google.android.gcm.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.example.someactivity" />
</intent-filter>
</receiver>
<service android:name=". GcmIntentService" />
</application>
</manifest>
And the receiver I used :
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString("Some Message");
Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
Showing received message
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
Releasing wake lock
WakeLocker.release();
}
};
Why didn’t I instantiate the sink, I’ve changed the package name.
Solution
Add gcm.jar to your libs folder.