我按照这里的教程http://developer.android.com/google/gcm/gs.html来设置 GCM。我目前正在尝试注册设备。但是,由于某种原因,应用程序似乎总是在 gcm = GoogleCloudMessaging.getInstance(this); 处崩溃。
堆栈跟踪似乎指向以下内容:06-18 13:42:20.909: I/dalvikvm(11613): 找不到方法 com.google.android.gms.gcm.GoogleCloudMessaging.getInstance,引用自方法 pushNotification.PushNotification$1 。跑
这是我到目前为止所拥有的样本
public PushNotification(Context c, Activity activity)
{
context = c;
this.activity = activity;
regid = getRegistrationId(context);
if (regid.length() == 0) {
Log.d("IN PUSHNOTIFICATION ","NOT REGISTERED. REGISTERING NOW.....");
registerBackground();
}
Log.d("IN PUSHNOTIFICATION ","REGISTRATION COMPLETE.....");
Log.d("IN PUSHNOTIFICATION ","REGISTRATION ID IS: " + regid);
gcm = GoogleCloudMessaging.getInstance(activity); //never reaches this code
}
private void registerBackground() {
Thread thread = new Thread(new Runnable()
{
public void run()
{
String msg = "";
try {
if (gcm == null) {
Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. gcm == NULL");
gcm = GoogleCloudMessaging.getInstance(context);
Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. AFTER gcm.GETINSTANCE");
}
Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. BEFORE gcm.register");
regid = gcm.register(SENDER_ID);
msg = "Device registered, registration id=" + regid;
Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. regid == " + regid);
// You should send the registration ID to your server over HTTP,
// so it can use GCM/HTTP or CCS to send messages to your app.
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the message
// using the 'from' address in the message.
// Save the regid - no need to register again.
setRegistrationId(context, regid);
} catch (IOException ex) {
Log.d("IN PUSHNOTIFICATION", "IN BACKGROUND. EXCEPTION ERROR ERROR: ex = " + ex.getMessage());
msg = "Error :" + ex.getMessage();
}
}
}
);
thread.start();
}
这是完整的堆栈跟踪
06-18 13:42:20.909:D/IN 推送通知(11613):未注册。现在注册..... 06-18 13:42:20.909: I/dalvikvm(11613): 找不到方法 com.google.android.gms.gcm.GoogleCloudMessaging.getInstance,引用自方法 pushNotification.PushNotification$1.run 06-18 13:42:20.909: W/dalvikvm(11613): VFY: 无法解析静态方法 5967: Lcom/google/android/gms/gcm/GoogleCloudMessaging;.getInstance (Landroid/content/Context;)Lcom/google /android/gms/gcm/GoogleCloudMessaging;06-18 13:42:20.909: D/dalvikvm(11613): VFY: 在 0x0015 处替换操作码 0x71 06-18 13:42:20.909: I/dalvikvm(11613): 找不到方法 com.google.android.gms .gcm.GoogleCloudMessaging.register,引用自方法 pushNotification.PushNotification$1.run 06-18 13:42:20.909:W/dalvikvm(11613):VFY:无法解析虚拟方法 5969:Lcom/google/android/gms/gcm/GoogleCloudMessaging;.register ([Ljava/lang/String;)Ljava/lang/String; 06-18 13:42:20.909: D/dalvikvm(11613): VFY: 在 0x0039 处替换操作码 0x6e 06-18 13:42:20.909: D/IN PUSHNOTIFICATION(11613): 注册完成..... 06-18 13:42:20.909:D/IN PUSHNOTIFICATION(11613):注册 ID 是:06-18 13:42:20.909:D/AndroidRuntime(11613):关闭 VM 06-18 13:42:20.909:W/dalvikvm( 11613):threadid = 1:线程以未捕获的异常退出(组= 0x2b542210)06-18 13:42:20.909:D / IN PUSHNOTIFICATION(11613):在后台。gcm == NULL 06-18 13:42:20.909: W/dalvikvm(11613): threadid=20: 线程以未捕获的异常退出 (group=0x2b542210) 06-18 13:42:20.909: I/Process(11613):发送信号。PID:11613 SIG:9 06-18 13:42:20.919:I/ActivityManager(278):进程 com。