@Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
try {
// In the (unlikely) event that multiple refresh operations occur simultaneously,
// ensure that they are processed sequentially.
synchronized (TAG) {
// [START get_token]
// Initially this call goes out to the network to retrieve the token, subsequent calls
// are local.
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(token);
// Subscribe to topic channels
subscribeTopics(token);
// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();
// [END get_token]
}
} catch (Exception e) {
Log.d(TAG, "Failed to complete token refresh", e);
// If an exception happens while fetching the new token or updating our registration data
// on a third-party server, this ensures that we'll attempt the update at a later time.
sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
}
// Notify UI that registration has completed, so the progress indicator can be hidden.
Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
使用instanceID.getToken方法和设备未连接到互联网访问令牌时,应用程序崩溃了。
从清单
调用此服务<service
android:name="com.sample.gcmclient.RegistrationIntentService"
android:exported="false">
Logcat详细信息
06-03 12:50:46.070 3067-4207/? D/GCM﹕ GcmService start Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gms cmp=com.google.android.gms/.gcm.GcmService (has extras) } com.google.android.c2dm.intent.REGISTER
06-03 12:50:46.080 9974-9974/com.sample.gcmclient D/dalvikvm﹕ DexOpt: couldn't find field Landroid/os/Message;.sendingUid
06-03 12:50:46.080 9974-9974/com.sample.gcmclient W/dalvikvm﹕ VFY: unable to resolve instance field 116
06-03 12:50:46.080 9974-9974/com.sample.gcmclient D/dalvikvm﹕ VFY: replacing opcode 0x52 at 0x0000
06-03 12:50:46.130 3067-4223/? W/GCM﹕ Missmatched messenger
06-03 12:50:46.170 9974-10176/com.sample.gcmclient D/RegIntentService﹕ Failed to complete token refresh
java.io.IOException: SERVICE_NOT_AVAILABLE
at com.google.android.gms.iid.zzc.zzb(Unknown Source)
at com.google.android.gms.iid.zzc.zza(Unknown Source)
at com.google.android.gms.iid.InstanceID.zzc(Unknown Source)
at com.google.android.gms.iid.InstanceID.getToken(Unknown Source)
at com.sample.gcmclient.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:68)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
06-03 12:50:53.080 4378-4380/? D/dalvikvm﹕ GC_CONCURRENT freed 101K, 19% free 5091K/6248K, paused 2ms+2ms, total 17ms
06-03 12:50:53.150 4378-4420/? D/Finsky﹕ [179] AppStatesReplicator.handleContentSyncResponse: Completed 0 account content syncs with 0 successful.
06-03 12:50:53.160 4378-4378/? D/Finsky﹕ [1] 5.onFinished: Installation state replication succeeded.
06-03 12:51:11.940 3350-3354/? D/dalvikvm﹕ GC_CONCURRENT freed 468K, 22% free 4985K/6344K, paused 2ms+3ms, total 18ms
如何修复此错误。 谢谢 Shinurag KR