GCM无法启动手动注册

时间:2012-07-04 06:50:20

标签: android google-cloud-messaging

this example中,广播接收器启动意向服务。 在IntentService实例中,它启动一个名为runIntentInService的方法,此方法将启动一个PowerManager.WakeLock对象,而不是启动该服务。

现在,当我手动启动注册过程(来自mainActivity)时:

    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
    // sets the app name in the intent
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(mainActivity.this, 0, new Intent(), 0));
    registrationIntent.putExtra("sender", _senderID);
    startService(registrationIntent);

在IntentService中,PowerManager.WakeLock的对象尚未初始化,因此它为null。

我使用this code从mainActivity注册。 我应该使用the following吗?

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

谢谢!

1 个答案:

答案 0 :(得分:0)

GCMRegistrar只是一个辅助类,它与您提到的完全相同,即发送寄存器意图等。 因此无需更改代码。