Mayur Rathod
Mayur Rathod

Reputation: 394

LocationServices.SettingsApi calling onActivityResult with RESULT_CANCELED?

I have been struggling with new LocationServices.SettingsApi in one particular case. I have written code on SplashActivity for Location setting(Google GPS Dialog Box which turn on GPS automatically) its run perfectly on Lollipop but in lower version like Kitkat or Jelly Bean its not showing google dialog box its showing RESULT_CANCELED in onActivityResult.. :(

PFA Google Dialog Box Image Code SnippetGoogle Dialog Box for automatically turning GPS on

if(googleApiClient == null) {

        googleApiClient = new GoogleApiClient.Builder(MapsActivity.this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        googleApiClient.connect();

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);

        //**************************
        builder.setAlwaysShow(true); //this is the key ingredient
        //**************************

        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(googleApiClient, 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:
                        // 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(
                                   MapsActivity.this, 1000);
                        } 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;
                }
            }
        });             }

the above code is for automatically turning gps on

and below code is of onActivityResult

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    LatLng currentLoc = LocationProvider.currentLoc;

    switch (resultCode) {
        case Activity.RESULT_OK:
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            if(mLastLocation!=null)
            {
                currentLoc = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
            }

            Log.e(TAG, "RESULT OK");
            break;
        case Activity.RESULT_CANCELED:
            checkGPSstatus();
            Log.e(TAG, "RESULT_CANCELED");
            break;
        default:
            break;
    }

Upvotes: 1

Views: 2076

Answers (2)

bluebyte
bluebyte

Reputation: 560

Let's try to guess. Do you have launchMode set to singleInstance for your activity? If so, you are not allowing other activities to be part of your task.

Upvotes: 3

denis_lor
denis_lor

Reputation: 6547

Please re-take a look at this project on Github:

https://github.com/googlesamples/android-play-location

You can try to re-write your code following one of those examples because it works in KitKat and JellyBean as well as Lollipop.

I have already implemented it a few days ago and is working in this way.

Upvotes: 0

Related Questions