I am developing an Android application that transfers content to Chromecast. Sometimes in my implementation of com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks in the onConnected method I get
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
an exception.
Here is the stack trace:
FATAL EXCEPTION: main Process: com.joaomgcd.autocast, PID: 13771 java.lang.IllegalStateException: GoogleApiClient is not connected yet. at com.google.android.gms.internal.eg.a(Unknown Source) at com.google.android.gms.common.api.GoogleApiClient.b(Unknown Source) at com.google.android.gms.cast.Cast$CastApi$a.launchApplication(Unknown Source) at com.joaomgcd.autocast.media.MediaConnectionCallbacks.onConnected(MediaConnectionCallbacks.java:37) at com.google.android.gms.internal.dx.b(Unknown Source) at com.google.android.gms.common.api.GoogleApiClient.bn(Unknown Source) at com.google.android.gms.common.api.GoogleApiClient.f(Unknown Source) at com.google.android.gms.common.api.GoogleApiClient$2.onConnected(Unknown Source) at com.google.android.gms.internal.dx.b(Unknown Source) at com.google.android.gms.internal.dx.bT(Unknown Source) at com.google.android.gms.internal.dw$hb(Unknown Source) at com.google.android.gms.internal.dw$hb(Unknown Source) at com.google.android.gms.internal.dw$b.bR(Unknown Source) at com.google.android.gms.internal.dw$a.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
This only happens if I have already connected to GoogleApiClient and reconnected. Between two calls, I disconnect from the api client with the code below.
I assume this is a mistake. I'm right? Since I use the onConnected method, GoogleApiClient should already be connected.
What can I do to get around this? Should I just wait a while until GoogleApiClient is really connected?
I am doing this in a service, and here are the relevant bits:
when starting the service:
mMediaRouter.addCallback(mMediaRouteSelector, mediaCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
mediaCallback has this code:
@Override public void onRouteAdded(MediaRouter router, RouteInfo route) { super.onRouteAdded(router, route); if (route.getDescription().equals("Chromecast")) { ... mSelectedDevice = com.google.android.gms.cast.CastDevice.getFromBundle(route.getExtras()); ... castClientListener = new CastListener(context, apiClient); Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions.builder(mSelectedDevice, castClientListener); ... apiClient.set(new GoogleApiClient.Builder(context).addApi(Cast.API, apiOptionsBuilder.build()).addConnectionCallbacks(connectionCallback).addOnConnectionFailedListener(new MediaConnectionFailedListener(context)).build()); apiClient.get().connect(); } }
connectionCallback has this code:
@Override public void onConnected(final Bundle arg0) { ... Cast.CastApi.launchApplication(apiClient, UtilAutoCast.CHROMECAST_APP_ID, false).setResultCallback(connectionCallback); ... }
The code above is the part where the failure occurs.
And when I stop the service, I run this code:
if (mMediaRouter != null) { mMediaRouter.removeCallback(mediaCallback); mMediaRouter = null; } if (apiClient != null) { Cast.CastApi.stopApplication(apiClient); if (apiClient.isConnected()) { apiClient.disconnect(); apiClient = null; } }
Thanks in advance.