1

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

GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

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.

Community
  • 1
  • 1

1 Answers1

0

Please try going through this tutorial of Google Client Location in AndroidHive - Android Location API using Google Play Services.

As discussed in the given tutorial, you need to do below changes in your activity to get the user’s current location.

  1. First check for availability of Google Play Services by calling checkPlayServices() in onResume()

  2. Once play services are available on the device, build the GoogleApiClient by calling buildGoogleApiClient() method.

  3. Connect to google api client by calling mGoogleApiClient.connect() in onStart() method. By calling this, onConnectionFailed(), onConnected() and onConnectionSuspended() will be triggered depending upon the connection status.

  4. Once google api is successfully connected, displayLocation() should be called in onConnected() method to get the current location.

Additionally, you can also check the following possible reasons for alerts not working as expected as given in Troubleshoot the Geofence Entrance Event. These are:

  • Accurate location is not available inside your geofence or your geofence is too small.
  • Wi-Fi is turned off on the device. Make sure that the wifi and location is enabled on your device.
  • There is no reliable network connectivity inside your geofence.
  • Alerts can be late.

Important information and sample codes can be found in Creating and Monitoring Geofences and in the given tutorial.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22