0

I am new to android development and I have chosen to work with Java. I am learning by developing an app. The app requires the location of the user. Now, I encountered problem while enabling location from the app.

I could launch the location settings page from the app using android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS and have the user manually enable the location, but do not want my users to have that extra step whenever they open the app. So I want to enable location automatically with just a prompt similar to how Google Maps does it...

Desired functionality

enter image description here

I have been searching all over the internet to understand how to implement this, but with no luck. None of the answers are working and all of them use deprecated API. There is a severe lack of new resources on this subject on the internet.

I tested on Xiaomi Redmi Note 9 pro and Lenovo K10 Note. Please help and thanks in advance...

I tried to put together a piece of code (which does not work and I do not know why) by resolving outdated lines of code with new alternatives.

public Task<LocationSettingsResponse> EnableLocation(int priority)
{
    LocationRequest.Builder locationRequest = new LocationRequest.Builder(priority);
    LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder();
        
    locationSettingsRequestBuilder.addLocationRequest(locationRequest.build());
    locationSettingsRequestBuilder.setAlwaysShow(true);
        
    SettingsClient settingsClient = LocationServices.getSettingsClient(context);
    return settingsClient.checkLocationSettings(locationSettingsRequestBuilder.build());
}

I tried deprecated solutions as well but those too are not able too enable the location settings like Google Maps.

One of the deprecated solutions that I tried had many upvotes on Stack Overflow. This did not work either.

Deprecated answers

I asked ChatGPT as well but all I got was outdated answers...

private void checkLocationSettings() {
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);

    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());

    task.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // Location settings are satisfied, you can proceed with location-related tasks
            // For example, start location updates
            startLocationUpdates();
        }
    });

    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof ResolvableApiException) {
                try {
                    // Location settings are not satisfied, but can be resolved by showing a dialog to the user
                    ResolvableApiException resolvable = (ResolvableApiException) e;
                    resolvable.startResolutionForResult(MainActivity.this, REQUEST_LOCATION_SETTINGS);
                } catch (IntentSender.SendIntentException sendEx) {
                    // Ignore the error
                }
            }
        }
    });
}
Younes Charfaoui
  • 1,059
  • 3
  • 10
  • 20
Zafar _
  • 1
  • 3
  • "all I got was outdated answers" -- what makes you think that the second code block is outdated? It appears to be covered in [the documentation](https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient). – CommonsWare Jun 03 '23 at 11:19
  • It says LocationRequest.create() is deprecated and to use LocationRequest.Builder() instead. Which I did as you can see in the first code block. – Zafar _ Jun 03 '23 at 13:50

1 Answers1

0

It says LocationRequest.create() is deprecated

The documentation for LocationRequest.create() has:

Use LocationRequest.Builder instead.

LocationRequest.Builder is not deprecated at this time, according to its documentation.

So, this:

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

turns into:

    LocationRequest locationRequest = new LocationRequestBuilder(0L)
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        .build();

(where you might replace 0L with the actual interval period in milliseconds, if you intend to use this LocationRequest for getting locations)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491