Search code examples
javaandroid-studioexceptiongps

enable GPS programmatically problem after gms:play-services-location upgrading to version 21.0.0


Before the last update of com.google.android.gms:play-services-location to version 21.0.0 I could use the next code to activate the GPS device:

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(Priority.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(Priority.PRIORITY_HIGH_ACCURACY);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

task.addOnFailureListener((Activity) context, e -> {
    if (e instanceof ResolvableApiException) {
        try {
            ResolvableApiException resolvable = (ResolvableApiException) e;
            resolvable.startResolutionForResult((Activity) context,
                    23);
        } catch (IntentSender.SendIntentException sendEx) {
            sendEx.printStackTrace();
        }
    }
});

After updating to version 21 my code looks alike:

LocationRequest locationRequest = 
        new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
            .setWaitForAccurateLocation(false)
            .setIntervalMillis(10000)
            .setPriority(Priority.PRIORITY_HIGH_ACCURACY)
            .build();
                            
                          
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
        .addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(context);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

task.addOnFailureListener((Activity) context, e -> {
    if (e instanceof ResolvableApiException) {
        try {
            ResolvableApiException resolvable = (ResolvableApiException) e;
            resolvable.startResolutionForResult((Activity) context,
                    23);
        } catch (IntentSender.SendIntentException sendEx) {
            sendEx.printStackTrace();
        }
    }
});

How can you see just locationRequest suffered modifier replacing create() with builder()?

The problem is that GPS activating menu is never displayed anymore. Is looks line the new exception is not ResolvableApiException.

Any idea how can I use enable GPS messages again?

I don't want to use the settings way:

((ActivityHome)context).startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

Solution

  • The latest version has this problem, choose a lower version.

    dependencies
    {
     implementation 'com.google.android.gms:play-services-location:21.0.0'
    }
    

    change to...

    dependencies
    {
        implementation 'com.google.android.gms:play-services location:20.0.0'
    
    }