I have been stuck on this for over a week now and I can't seem to figure out what exactly it is that I'm doing wrong. I have read the following questions, none of which seem to work for me:
Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet
google api client callback is never called
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet
What I'm trying to do is use the LocationServices API in combination with the Geofence class to check if the user is in a specified area. The problem seems to lie in communicating with the Google API Client and/or Locationservices API.
I have declared the following in my manifest:
<service android:name=".GeofenceTransitionsIntentService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="value of my key" />
</application>
I have 2 Java methods declared in my project which interact with the GoogleApiClient:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public void startApiClient(){
if (mGoogleApiClient.isConnected()) {
Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
return;
//Of course the Toast is supposed to fire of when the user cannot
//connect to the google api. However when I make this code to run
//when the user cannot connect to google play services it just shows
//the toast and does not report me the error which is why I in this
//case made this code to run when the user is connected to google play services
}
try {
LocationServices.GeofencingApi.addGeofences
(mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent()).setResultCallback(this); // Result processed in onResult(). This is also the line where the app seems to crash because it is unable to connect to the google api client
} catch (SecurityException securityException) {
securityException.notify();
}
}
I then call the buildGoogleApiClient() in my onCreate() method and call startApiClient() in my onStart() method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
…
buildGoogleApiClient();
}
_
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
startApiClient();
}
And in case neccessary, the onResult() method:
public void onResult(Status status) {
if (status.isSuccess()) {
// Update state and save in shared preferences.
mGeofencesAdded = !mGeofencesAdded;
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(Constants.GEOFENCES_ADDED_KEY, mGeofencesAdded);
editor.apply();
}
The strange thing about this is that a connection actually does get instantiated for some time. When I tell the program to show the Toast when the user is not connected, my Android Monitor tells me that a connection has been made but the Toast still gets triggered. However when I tell the program to show the Toast when the user is connected to the Google API Client the app crashes and tells me that the Google API Client has not connected yet.
Thanks in advance for taking time out of your day to help me with this. Please forgive me if I'm overseeing something very obvious.