0

So I notice Google Map was able to prompt the user to enable the GPS. I tried this method below and I got the prompt once and then nothing... Why aren't the onActivityResult() method isn't called anymore?

public void checkLocationEnable() {
    Log.e(TAG, "Here");
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);

    final PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                    builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            final LocationSettingsStates state = locationSettingsResult.getLocationSettingsStates();
            Log.e(TAG, "state:" + state);
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.e(TAG, "HERE-1");
                    // All location settings are satisfied. The client can
                    // initialize location requests here.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.e(TAG, "REQUIRED");
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                getActivity(),
                                GenericActivity.REQUEST_LOCATION);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way
                    // to fix the settings so we won't show the dialog.
                    Log.e(TAG, "UNAVALAIBLE");

                    break;
            }
        }
    });
}
Jaythaking
  • 2,136
  • 2
  • 21
  • 57
  • You need to overide a onActivityResult method so that when user respond to a dialog box this method will receive a result. – Aman Jain Jun 24 '16 at 11:11
  • I do override it in the ActivityHome – Jaythaking Jun 24 '16 at 14:32
  • [this](http://stackoverflow.com/questions/843675/how-do-i-find-out-if-the-gps-of-an-android-device-is-enabled/843716#comment63478757_843716) does what you're asking for. – TDG Jun 24 '16 at 18:45

1 Answers1

2

As far as I understand you try to enable the location.

Step by step process.

First you need to set a GoogleApiClient and implements GoogleApiClient CallBackMethods.

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
        mGoogleApiClient.connect();   
}

CallBack Methods

@Override
public void onConnected(Bundle bundle) {
    Log.d("OnConnected", "Connection successful");
    settingsrequest();
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}

As soon as the googleapiclient connects, onconnected method will invoke and settingsRequest method will call.

protected static final int REQUEST_CHECK_SETTINGS = 0x1;
LocationRequest locationRequest;


public void settingsrequest() {
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setNumUpdates(1);
    locationRequest.setExpirationDuration(20000);
    locationRequest.setFastestInterval(500);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    builder.setAlwaysShow(true); //this is the key ingredient
    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS: {
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
                }
                // All location settings are satisfied. The client can initialize location
                // requests here.
                break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MainTab.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    break;
            }
        }
    });
}

status.startResolutionForResult(MainTab.this, REQUEST_CHECK_SETTINGS) will show a dialog requesting location.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
   // Check for the integer request code originally supplied to startResolutionForResult().
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
                    break;
                case Activity.RESULT_CANCELED:
                    break;
            }
            break;
    }
}

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this) will set a listener for the location cahnged.

As the location changed onLocationChanged method will invoke.

 @Override
public void onLocationChanged(Location location) {
      prevLocation  = location;
      //Do you work
}

Hope this will helps you.

Aman Jain
  • 2,851
  • 1
  • 16
  • 32