Enabling Location Mode High Accuracy or Battery Saving, Programmatically, Without User Needing to Visit Settings

Enabling Location mode High Accuracy or Battery saving, programmatically, without user needing to visit Settings

UPDATE 3:

Prompt the user to change location settings
as @patrickandroid, mentioned in comments, the link from second update is broken

GoogleSamples; android Location and options - for code reference.

UPDATE 2:

GoogleSamples; - for code reference.

This sample builds on the LocationUpdates sample included in this
repo, and allows the user to update the device's location settings
using a location dialog.

Uses the SettingsApi to ensure that the device's system settings are
properly configured for the app's location needs.


UPDATE 1:

Google Play services, Version 7.0 (March 2015) released...

Location settings - While the FusedLocationProviderApi combines multiple sensors to give you the optimal location, the accuracy of the
location your app receives still depends greatly on the settings
enabled on the device (GPS, wifi, airplane mode, and others). Using
the new SettingsApi class, you can bring up a Location Settings dialog
which displays a one-touch control for users to change their settings
without leaving your app.

Using the public interface SettingsApi

  • Determine if the relevant system settings are enabled on the device to carry out the desired location request.
  • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.


Leaving the previous part for reference:

Update/Answer
For everybody looking for this answer, Google Play Services 7.0
It Adds APIs For Detecting Places And Connecting To Nearby Devices, Improves On Mobile Ads, Fitness Data, Location Settings, And More

In Google Play services 7.0, we’re introducing a standard mechanism to
check that the necessary location settings are enabled for a given
LocationRequest to succeed. If there are possible improvements, you
can display a one touch control for the user to change their settings
without leaving your app.

exactly this

This API provides a great opportunity to make for a much better user
experience, particularly if location information is critical to the
user experience of your app such as was the case with Google Maps when
they integrated the Location Settings dialog and saw a dramatic
increase in the number of users in a good location state.

Source: Android developers blog: Google Play services 7.0 - Places Everyone!

SDK Coming Soon!
We will be rolling out Google Play services 7.0 over
the next few days. Expect an update to this blog post, published
documentation, and the availability of the SDK once the rollout is
completed.

will update the programmatic l-o-c after implementation

how to change the location mode to high accuracy/battery saving form it default mode(device only)

Before requesting location updates, you need to check for correct location settings as per your requirement.

Create a LocationSettingsRequest.Builder and add all of the LocationRequests that the app will be using:

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

Check whether location settings are satisfied, using

PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleClient, builder.build());

When the PendingResult returns, the client can check the location settings by looking at the status code from the LocationSettingsResult object.

 result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates = 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,
REQUEST_CHECK_SETTINGS);
} catch (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 result of the dialog will be returned via onActivityResult(int, int, Intent)

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
...
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
...
break;
default:
break;
}
break;
}
}

For reference, read SettingsApi document which explains the case very well.

Alternatively, you can directly redirect the user to location settings page.

int locationMode = Settings.Secure.getInt(activityUnderTest.getContentResolver(), Settings.Secure.LOCATION_MODE);

Check location_mode for possible return values.

if(locationMode == LOCATION_MODE_HIGH_ACCURACY) {
//request location updates
} else { //redirect user to settings page
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}

Turn on Location Providers Programmatically in Android

No, it is not,
but you can open the Location services settings window:

context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));


Related Topics



Leave a reply



Submit