Skip to content Skip to sidebar Skip to footer

Unclear Android Current Location Retrieval Tutorial

I am recycling the code provided to check that the user's device has Google Play services on it before acquiring location data from http://developer.android.com/training/location/r

Solution 1:

The tutorial is misleading. If you want to check google play services exist do the following.

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS) {
  GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}

This will automatically show an appropriate error dialog if it doesn't in exist.

To your second problem. The remainder of the tutorial does need to be followed. You do need to implement GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener if you want to create the the locationclient using new LocationClient(this, this, this);

Note: do not try to use the locationclient until after the onConnected method is called in your callback.

Solution 2:

Following the tutorial I ran into the same errors, however the code sample provided seems to be correctly implemented.

/**
 * Verify that Google Play services is available before making a request.
 *
 * @return true if Google Play services is available, otherwise false
 */privatebooleanservicesConnected() {

    // Check that Google Play services is availableintresultCode=
            GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    // If Google Play services is availableif (ConnectionResult.SUCCESS == resultCode) {
        // In debug mode, log the status
        Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available));

        // Continuereturntrue;
    // Google Play services was not available for some reason
    } else {
        // Display an error dialogDialogdialog= GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0);
        if (dialog != null) {
            ErrorDialogFragmenterrorFragment=newErrorDialogFragment();
            errorFragment.setDialog(dialog);
            errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG);
        }
        returnfalse;
    }
}

http://developer.android.com/shareables/training/LocationUpdates.zip

Post a Comment for "Unclear Android Current Location Retrieval Tutorial"