应用程序启动时出现 GCM 错误
GCM error on app start
这是我的RegisterGCM
class
public class RegisterGCM {
private static final String PROJECT_ID = "589104089214";
// This tag is used in Log.x() calls
private static final String TAG = "otp_verification.";
// This string will hold the lengthy registration id that comes
// from GCMRegistrar.register()
private String regId = "";
// These strings are hopefully self-explanatory
private String registrationStatus = "Not yet registered";
private String broadcastMessage = "No broadcast message";
// This intent filter will be set to filter on the string
// "GCM_RECEIVED_ACTION"
IntentFilter gcmFilter;
Context ctx;
// textviews used to show the status of our app's registration, and the
// latest
// broadcast message.
// This broadcastreceiver instance will receive messages broadcast
// with the action "GCM_RECEIVED_ACTION" via the gcmFilter
public String getRegId() {
return regId;
}
public void setRegId(String regId) {
this.regId = regId;
}
public String getRegistrationStatus() {
return registrationStatus;
}
public void setRegistrationStatus(String registrationStatus) {
this.registrationStatus = registrationStatus;
}
public String getBroadcastMessage() {
return broadcastMessage;
}
public void setBroadcastMessage(String broadcastMessage) {
this.broadcastMessage = broadcastMessage;
}
public IntentFilter getGcmFilter() {
return gcmFilter;
}
public void setGcmFilter(IntentFilter gcmFilter) {
this.gcmFilter = gcmFilter;
}
public Context getCtx() {
return ctx;
}
public void setCtx(Context ctx) {
this.ctx = ctx;
}
public BroadcastReceiver getGcmReceiver() {
return gcmReceiver;
}
public void setGcmReceiver(BroadcastReceiver gcmReceiver) {
this.gcmReceiver = gcmReceiver;
}
public static String getProjectId() {
return PROJECT_ID;
}
public static String getTag() {
return TAG;
}
// A BroadcastReceiver must override the onReceive() event.
private BroadcastReceiver gcmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (broadcastMessage != null) {
// display our received message
}
}
};
public RegisterGCM(Context ctx) {
super();
this.ctx = ctx;
}
public void registerClient() {
// TODO Auto-generated method stub
try {
// Check that the device supports GCM (should be in a try / catch)
GCMRegistrar.checkDevice(ctx);
// Check the manifest to be sure this app has all the required
// permissions.
GCMRegistrar.checkManifest(ctx);
// Get the existing registration id, if it exists.
regId = GCMRegistrar.getRegistrationId(ctx);
if (regId.equals("")) {
registrationStatus = "Registering...";
// register this device for this project
GCMRegistrar.register(ctx, PROJECT_ID);
regId = GCMRegistrar.getRegistrationId(ctx);
registrationStatus = "Registration Acquired";
// This is actually a dummy function. At this point, one
// would send the registration id, and other identifying
// information to your server, which should save the id
// for use when broadcasting messages.
sendRegistrationToServer();
} else {
registrationStatus = "Already registered";
}
} catch (Exception e) {
e.printStackTrace();
registrationStatus = e.getMessage();
}
Log.d(TAG, registrationStatus);
// tvRegStatusResult.setText(registrationStatus);
// This is part of our CHEAT. For this demo, you'll need to
// capture this registration id so it can be used in our demo web
// service.
Log.d(TAG, regId);
}
private void sendRegistrationToServer() {
// TODO Auto-generated method stub
}
// NOTE the call to GCMRegistrar.onDestroy()
public void onDestroy() {
GCMRegistrar.onDestroy(ctx);
}
public void stop(Context ctx) {
GCMRegistrar.unregister(ctx);
}
}
这是我的 LogCat
启动应用程序时显示的内容
06-18 12:15:03.338: V/GCMBroadcastReceiver(24212): onReceive: com.google.android.c2dm.intent.RECEIVE
06-18 12:15:03.338: V/GCMBroadcastReceiver(24212): GCM IntentService class: com.datavsn.QuickTransfer.GCMIntentService
06-18 12:15:03.338: V/GCMBaseIntentService(24212): Acquiring wakelock
06-18 12:15:03.348: E/BroadcastReceiver(24212): BroadcastReceiver trying to return result during a non-ordered broadcast
06-18 12:15:03.348: E/BroadcastReceiver(24212): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:783)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.content.BroadcastReceiver.setResult(BroadcastReceiver.java:658)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:47)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2667)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread.access00(ActivityThread.java:166)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.os.Looper.loop(Looper.java:136)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread.main(ActivityThread.java:5584)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at java.lang.reflect.Method.invoke(Method.java:515)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at dalvik.system.NativeStart.main(Native Method)
06-18 12:15:03.348: D/GCMIntentService(24212): GCMIntentService init
06-18 12:15:03.358: D/GCMIntentService(24212): Message Received
06-18 12:15:03.358: V/GCMBaseIntentService(24212): Releasing wakelock
我刚刚启动了我的应用程序,它没有 gcm id 所以它怎么能像我的 logcat 最后 2 行建议的那样接收消息,还有我获取 id 的代码是当我移动到我的登录页面。
该应用程序在我第一次启动时崩溃,然后当我再次启动它时它运行顺利,没有上述异常。
这看起来像是一个非常旧版本的 GCM 库的已知问题。我建议更新到最新版本,详情如下:https://developers.google.com/cloud-messaging/android/client
这是我的RegisterGCM
class
public class RegisterGCM {
private static final String PROJECT_ID = "589104089214";
// This tag is used in Log.x() calls
private static final String TAG = "otp_verification.";
// This string will hold the lengthy registration id that comes
// from GCMRegistrar.register()
private String regId = "";
// These strings are hopefully self-explanatory
private String registrationStatus = "Not yet registered";
private String broadcastMessage = "No broadcast message";
// This intent filter will be set to filter on the string
// "GCM_RECEIVED_ACTION"
IntentFilter gcmFilter;
Context ctx;
// textviews used to show the status of our app's registration, and the
// latest
// broadcast message.
// This broadcastreceiver instance will receive messages broadcast
// with the action "GCM_RECEIVED_ACTION" via the gcmFilter
public String getRegId() {
return regId;
}
public void setRegId(String regId) {
this.regId = regId;
}
public String getRegistrationStatus() {
return registrationStatus;
}
public void setRegistrationStatus(String registrationStatus) {
this.registrationStatus = registrationStatus;
}
public String getBroadcastMessage() {
return broadcastMessage;
}
public void setBroadcastMessage(String broadcastMessage) {
this.broadcastMessage = broadcastMessage;
}
public IntentFilter getGcmFilter() {
return gcmFilter;
}
public void setGcmFilter(IntentFilter gcmFilter) {
this.gcmFilter = gcmFilter;
}
public Context getCtx() {
return ctx;
}
public void setCtx(Context ctx) {
this.ctx = ctx;
}
public BroadcastReceiver getGcmReceiver() {
return gcmReceiver;
}
public void setGcmReceiver(BroadcastReceiver gcmReceiver) {
this.gcmReceiver = gcmReceiver;
}
public static String getProjectId() {
return PROJECT_ID;
}
public static String getTag() {
return TAG;
}
// A BroadcastReceiver must override the onReceive() event.
private BroadcastReceiver gcmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (broadcastMessage != null) {
// display our received message
}
}
};
public RegisterGCM(Context ctx) {
super();
this.ctx = ctx;
}
public void registerClient() {
// TODO Auto-generated method stub
try {
// Check that the device supports GCM (should be in a try / catch)
GCMRegistrar.checkDevice(ctx);
// Check the manifest to be sure this app has all the required
// permissions.
GCMRegistrar.checkManifest(ctx);
// Get the existing registration id, if it exists.
regId = GCMRegistrar.getRegistrationId(ctx);
if (regId.equals("")) {
registrationStatus = "Registering...";
// register this device for this project
GCMRegistrar.register(ctx, PROJECT_ID);
regId = GCMRegistrar.getRegistrationId(ctx);
registrationStatus = "Registration Acquired";
// This is actually a dummy function. At this point, one
// would send the registration id, and other identifying
// information to your server, which should save the id
// for use when broadcasting messages.
sendRegistrationToServer();
} else {
registrationStatus = "Already registered";
}
} catch (Exception e) {
e.printStackTrace();
registrationStatus = e.getMessage();
}
Log.d(TAG, registrationStatus);
// tvRegStatusResult.setText(registrationStatus);
// This is part of our CHEAT. For this demo, you'll need to
// capture this registration id so it can be used in our demo web
// service.
Log.d(TAG, regId);
}
private void sendRegistrationToServer() {
// TODO Auto-generated method stub
}
// NOTE the call to GCMRegistrar.onDestroy()
public void onDestroy() {
GCMRegistrar.onDestroy(ctx);
}
public void stop(Context ctx) {
GCMRegistrar.unregister(ctx);
}
}
这是我的 LogCat
启动应用程序时显示的内容
06-18 12:15:03.338: V/GCMBroadcastReceiver(24212): onReceive: com.google.android.c2dm.intent.RECEIVE
06-18 12:15:03.338: V/GCMBroadcastReceiver(24212): GCM IntentService class: com.datavsn.QuickTransfer.GCMIntentService
06-18 12:15:03.338: V/GCMBaseIntentService(24212): Acquiring wakelock
06-18 12:15:03.348: E/BroadcastReceiver(24212): BroadcastReceiver trying to return result during a non-ordered broadcast
06-18 12:15:03.348: E/BroadcastReceiver(24212): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:783)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.content.BroadcastReceiver.setResult(BroadcastReceiver.java:658)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at com.google.android.gcm.GCMBroadcastReceiver.onReceive(GCMBroadcastReceiver.java:47)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2667)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread.access00(ActivityThread.java:166)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.os.Looper.loop(Looper.java:136)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at android.app.ActivityThread.main(ActivityThread.java:5584)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at java.lang.reflect.Method.invoke(Method.java:515)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
06-18 12:15:03.348: E/BroadcastReceiver(24212): at dalvik.system.NativeStart.main(Native Method)
06-18 12:15:03.348: D/GCMIntentService(24212): GCMIntentService init
06-18 12:15:03.358: D/GCMIntentService(24212): Message Received
06-18 12:15:03.358: V/GCMBaseIntentService(24212): Releasing wakelock
我刚刚启动了我的应用程序,它没有 gcm id 所以它怎么能像我的 logcat 最后 2 行建议的那样接收消息,还有我获取 id 的代码是当我移动到我的登录页面。 该应用程序在我第一次启动时崩溃,然后当我再次启动它时它运行顺利,没有上述异常。
这看起来像是一个非常旧版本的 GCM 库的已知问题。我建议更新到最新版本,详情如下:https://developers.google.com/cloud-messaging/android/client