31

In my app if the user doesn't have the location turned on I am prompting with a dialog and then trying to return that result (probably incorrectly) by overriding on activity result.

This is inside a fragment so not sure how that changes things:

This is how I am calling it the dialog with startResolutionForResult:

public void checkDeviceLocationIsOn()
        {
            System.out.println("Test running setting request" );
            LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
            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:
                            // All location settings are satisfied.
                            System.out.println("Test setting all fine starting location request" );
                            getLocation();
                            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(getActivity(), LOCATION_SETTINGS_REQUEST_CODE);
                                System.out.println("Test setting not met starting dialog to prompt user" );
                            } 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;
                    }
                }
            });
        }

And then I try to get the result like this below it (This never gets called):

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
            case LOCATION_SETTINGS_REQUEST_CODE:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        System.out.println("test user has turned the gps back on");
                        getLocation();
                        break;
                    case Activity.RESULT_CANCELED:

                        System.out.println("test user has denied the gps to be turned on");
                        Toast.makeText(getActivity(), "Location is required to order stations", Toast.LENGTH_SHORT).show();
                        break;
                }
                break;
        }
    }

Thanks in advance for your help

Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

4 Answers4

61

Call the fragment method

startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);

instead of

status.startResolutionForResult(...)

Vince
  • 1,570
  • 3
  • 27
  • 48
Badhrinath Canessane
  • 3,408
  • 2
  • 24
  • 38
  • Nothing is more stable than api from Google. Years are passing and everything is still actual xD – Karol Kulbaka Oct 21 '21 at 19:52
  • Interesting, this one worked but can you please provide with more information on why we should use this ? (just to have an idea). Thank you! – Notron Aug 08 '22 at 16:26
38

To start resolution for result in your fragment you should use the new API

first, create the ActivityResultLanucher

private val resolutionForResult =
    registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { activityResult ->
        if (activityResult.resultCode == RESULT_OK)
            //startLocationUpdates() or do whatever you want
        else {
            showMessage("we can't determine your location")
        }
    }

then use it like this

val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
resolutionForResult.launch(intentSenderRequest)
Amr
  • 1,068
  • 12
  • 21
1

in Super Activity Perform this Method...

in MainActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    final int REQUEST_CHECK_SETTINGS = 100;//check GPS setting
    Log.d("onActivityResult()", Integer.toString(resultCode));

    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode)
    {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode)
            {
                case Activity.RESULT_OK:
                {
                    // All required changes were successfully made
                    Toast.makeText(this, "Location enabled by user!", Toast.LENGTH_LONG).show();
                    ContactFragment.isGPSEnabled=true;
                    ContactFragment.click();
                    break;
                }
                case Activity.RESULT_CANCELED:
                {
                    // The user was asked to change settings, but chose not to
                    Toast.makeText(this, "Location not enabled, user cancelled.", Toast.LENGTH_LONG).show();
                    ContactFragment.isGPSEnabled=false;
                    break;
                }
                default:
                {
                    break;
                }
            }
            break;
    }

}

inside ContactFragment

@Override
public void onConnected(@Nullable Bundle bundle) {

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(30 * 1000);
    mLocationRequest.setFastestInterval(5 * 1000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);

    result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(this);
}
  @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(
                        getActivity(),
                        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;
    }
}
aj dodiya
  • 11
  • 1
0

Maybe an easy solution is to use two activities and show one like a dialog by using any dialog theme on the Manifest

<activity
        android:name=".LocationDialogActivity"
        android:theme="@style/DialogStyle"></activity>

them just use the onActivityResult as you used it always.

DialogActivity

 private void setLocation() {
    Intent intentResult = new Intent();
    intentResult.putExtra("latitude", "valueLat");
    intentResult.putExtra("longitude", "valueLon");
    setResult(RESULT_OK, intentResult);
    finish();
}

MainActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            /*Take your data*/
            latitude = data.getExtras().getString("latitude");
            longitude = data.getExtras().getString("longitude");
        }
}