Android云消息传递 - 我应该定义自己的GCMRegistrar.java类还是重用Google

时间:2012-10-17 13:09:00

标签: android google-cloud-messaging

我正在研究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 -->

1 个答案:

答案 0 :(得分:2)

  

我添加它的原因是当我没有它时,当我尝试使用某些方法时:   GCMRegistrar.setRetryReceiverClassName(MyClass的);在GCMBroadcastReceiver类中,我收到一个语法错误,该方法在GCMRegistrar类中不可见。

那是因为没有这样的方法。您不会在Android开发人员文档中找到对此类方法的引用。

  

所以我不确定是否应该导入com.google.android.gcm.GCMRegistrar

是的,不要尝试使用不存在的方法。可以在以下位置找到GCM客户端库的JavaDoc:http://developer.android.com/guide/google/gcm/client-javadoc/index.html

  

无法实例化服务com.problemio.GCMIntentService:java.lang.InstantiationException:com.problemio.GCMIntentService

因为com.problemio.GCMIntentService中存在错误,例如没有公共零参数构造函数。

以下示例项目演示了GCM客户端库的使用:https://github.com/commonsguy/cw-omnibus/tree/master/Push/GCMClient