我正在研究 Google 为云消息传递提供的示例。我有这个代码开始:
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");
}
而且这段代码永远不会上GCMRegistrar.checkManifest(this);
线。
我得到的例外是
Unable to instantiate service com.problemio.GCMIntentService:
java.lang.InstantiationException: com.problemio.GCMIntentService
我做的一件事是实现我自己的 GCMRegistrar.java 类,我从一个例子中得到。我添加它的原因是当我没有它时,当我尝试使用一些方法时:
GCMRegistrar.setRetryReceiverClassName(myClass); 在 GCMBroadcastReceiver 类中,我收到一个语法错误,即此方法在 GCMRegistrar 类中不可见。
所以我不确定是否应该导入 com.google.android.gcm.GCMRegistrar; 或者我是否应该在本地制作自己的版本。你们有什么建议?
当我在本地拥有它时,我得到异常的原因可能是什么:
无法实例化服务 com.problemio.GCMIntentService:java.lang.InstantiationException:com.problemio.GCMIntentService
ps - 所有测试都是在设备而不是模拟器上完成的。
编辑:
这是最新的例外:
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)
这是我最新的 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);
}
}
这是我的清单:
<?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 -->