I am working through an example that Google provides for Cloud Messaging. I have this code to start with:
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals(""))
{
GCMRegistrar.register(this, "my_id");
}
else
{
//Log.v(TAG, "Already registered");
}
and this code never gets to the GCMRegistrar.checkManifest(this); line.
The exception I was getting was
Unable to instantiate service com.problemio.GCMIntentService:
java.lang.InstantiationException: com.problemio.GCMIntentService
One thing I had done was to implement my own GCMRegistrar.java class which I got from an example. The reason I added it was that when I didn’t have it, when I tried to use some methods like:
GCMRegistrar.setRetryReceiverClassName(myClass); in the GCMBroadcastReceiver class, I was getting a syntax error that this method is not visible in the GCMRegistrar class.
So I am not sure whether I should import com.google.android.gcm.GCMRegistrar; or if I should just make my own version of it locally. What do you guys suggest?
And when I had it locally, what could be the reason I got the exception:
Unable to instantiate service com.problemio.GCMIntentService:
java.lang.InstantiationException: com.problemio.GCMIntentService
ps – all testing was done on a device and not the simulator.
EDIT:
Here is the latest exception:
java.lang.RuntimeException: Unable to instantiate service com.problemio.GCMIntentService: java.lang.InstantiationException: com.problemio.GCMIntentService
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2201)
at android.app.ActivityThread.access$2500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1102)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.InstantiationException: com.problemio.GCMIntentService
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2198)
... 10 more
java.lang.InstantiationException: com.problemio.GCMIntentService
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2198)
at android.app.ActivityThread.access$2500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1102)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4293)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
and here is my latest GCMIntentService:
package com.problemio;
import static com.google.android.gcm.GCMConstants.ERROR_SERVICE_NOT_AVAILABLE;
import static com.google.android.gcm.GCMConstants.EXTRA_ERROR;
import static com.google.android.gcm.GCMConstants.EXTRA_REGISTRATION_ID;
import static com.google.android.gcm.GCMConstants.EXTRA_SPECIAL_MESSAGE;
import static com.google.android.gcm.GCMConstants.EXTRA_TOTAL_DELETED;
import static com.google.android.gcm.GCMConstants.EXTRA_UNREGISTERED;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_MESSAGE;
import static com.google.android.gcm.GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK;
import static com.google.android.gcm.GCMConstants.VALUE_DELETED_MESSAGES;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import com.google.android.gcm.GCMBaseIntentService;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;
import utils.GCMConstants;
public abstract class GCMIntentService extends GCMBaseIntentService
{
public GCMIntentService()
{
super(ProblemioActivity.SENDER_ID);
}
@Override
protected void onRegistered(Context ctxt, String regId) {
Log.d(getClass().getSimpleName(), "onRegistered: " + regId);
Toast.makeText(this, regId, Toast.LENGTH_LONG).show();
}
@Override
protected void onUnregistered(Context ctxt, String regId) {
Log.d(getClass().getSimpleName(), "onUnregistered: " + regId);
}
@Override
protected void onMessage(Context ctxt, Intent message) {
Bundle extras=message.getExtras();
for (String key : extras.keySet()) {
Log.d(getClass().getSimpleName(),
String.format("onMessage: %s=%s", key,
extras.getString(key)));
}
}
@Override
protected void onError(Context ctxt, String errorMsg) {
Log.d(getClass().getSimpleName(), "onError: " + errorMsg);
}
@Override
protected boolean onRecoverableError(Context ctxt, String errorMsg) {
Log.d(getClass().getSimpleName(), "onRecoverableError: " + errorMsg);
return(true);
}
}
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.problemio"
android:versionCode="82"
android:versionName="2.2.82" >
<supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="16"/>
<!-- <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="xx"/> -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Required permission to use in-app billing. -->
<uses-permission android:name="com.android.vending.BILLING" />
<permission android:name="com.problemio.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.problemio.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- 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" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomTheme"
android:name="MyApplication"
android:debuggable="true"
>
<!-- For Google Cloud Messaging -->
<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.problemio" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
<!-- End of Google Cloud Messaging -->
That is because there is no such method. You will not find a reference to such a method in the Android developer documentation.
Yes, and do not try to use non-existent methods. The JavaDoc for the GCM client library can be found at: http://developer.android.com/guide/google/gcm/client-javadoc/index.html
Because there is a bug in
com.problemio.GCMIntentService, such as not having a public zero-argument constructor.Here is a sample project that demonstrates the use of the GCM client library: https://github.com/commonsguy/cw-omnibus/tree/master/Push/GCMClient