GoogleApiClient is throwing GoogleApiClient is not connected yet AFTER onConnected function getting called
I just noticed that you are creating the googleApiClient in onStartCommand(). This seems like a bad idea.
Let's say that your service gets triggered twice. Two googleApiClient objects will get created, but you'll only have reference to one. If the one whose reference you don't have executes its callback to onConnected(), you will be connected in that client but the client whose reference you actually do have could still be unconnected.
I suspect that's what's going on. Try moving your googleApiClient creation to onCreate and see if you get the same behavior.
GoogleApiClient is not connected yet, even though onConnected is called and I'm creating my GoogleApiClient in onCreate
Your problem is in the onResume
logic:
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
resumeLocationUpdates();
}
private void resumeLocationUpdates() {
Log.i("RESUMING", "RESUMING LOCATION UPDATES");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
The call to mGoogleApiClient.connect()
is asynchronous. It returns before the connect is finished, and you are requesting location updates before the client is connected. You need to move the requestLocationUpdates
call to the GoogleApiClient.onConnected
callback. After this event, your client is connected.
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
resumeLocationUpdates();
}
Android:GoogleApiClient is not connected yet?
Solved my problem because I use splash screen in my project put this code in splash screen activity :
mGoogleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet
You should ditch the threading and just create a second GoogleApiClient
. According to this post (https://stackoverflow.com/a/25190497/608347) the client isn't a heavy object so might as well avoid the confusing design and make things simple. Even if you dont go down this path you should strip out that #setClient
and #getClient
code and see if you get the same error when disconnecting from a single activity
GoogleApiClient not calling onConnected method inside service
You have to connect using mGoogleApiClient.connect() inside onStartCommand
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}else{
Toast.makeText(getApplicationContext(),"mGoogleApiClient is null",Toast.LENGTH_LONG).show();
}
return START_STICKY;
}
Related Topics
Open Specific Activity When Notification Clicked in Fcm
Calling Hidden API in Android to Turn Screen Off
Android: Detect Orientation Changed
Failed to Find Target with Hash String 'Android-22'
Mediaplayer.Setdatasource Causes Ioexception for Valid File
Firebase Cloud Messaging Send Click_Action
How to Make APK Secure. Protecting from Decompile
How to Align Android Toolbar Menu/Icons to the Left Like in Google Maps App
Why Is My Android Emulator Keyboard in Chinese Character Mode
How to Get Profile Like Gender from Google Signin in Android
How to Disable Status Bar Click and Pull Down in Android
Android Datepicker Min Max Date Before API Level 11
Android Studio: Gradle: Error: Cannot Find Symbol Variable
Passing Bitmap Between Two Activities