我已关注https://developers.google.com/cloud-messaging/android/start并使用https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart作为实施GCM的模板。
当应用程序以Internet连接启动时,它工作正常,但是,如果没有Internet,我的应用程序会陷入无限循环:
12-08 01:00:07.318 26039-27088/myapp E/RegIntentService: end of intent
12-08 01:00:07.327 26039-27090/myapp E/RegIntentService: onHandleIntent
12-08 01:00:07.339 26039-27090/myapp E/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 myapp.services.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:49)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.os.HandlerThread.run(HandlerThread.java:61)
12-08 01:00:07.339 26039-27090/myapp E/RegIntentService: end of intent
12-08 01:00:07.355 26039-27091/myapp E/RegIntentService: onHandleIntent
12-08 01:00:07.368 26039-27091/myapp E/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 myapp.services.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:49)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.os.HandlerThread.run(HandlerThread.java:61)
12-08 01:00:07.369 26039-27091/myapp E/RegIntentService: end of intent
最初来自onReceive
的{{1}}也会多次调用,但稍后会停止记录。
我的活动:
mRegistrationBroadcastReceiver
我的RegistrationIntentService:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "onReceive");
}
};
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Log.e(TAG, "Start RegistrationIntentService");
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
原因是什么?