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
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.
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
}
}
}
});
}