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

Exception or error:

Why i ask this:(also the reason for trying it in an app)

It happens when we use Google Maps in Lollipop. Even if the Location is disabled, it is turned on, in high accuracy mode after user’s input from the Maps app, without having to visit Settings.

A similar functionality can be achieved for enabling Bluetooth, where the action is initiated in my app; user needs to make a choice but user is not redirected to Settings, using:

startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));

which could be found on BluetoothAdapter, now we know there is no LocationAdapter, so i looked around gms->LocationServices, basically almost everything under Location API references, android.location.LocationManager but doesn’t seem anything like ACTION_REQUEST_ENABLE is available as yet.

Hope there is some other method for the same, and more people have tried it.

Please note:
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); does not work like that.

How to solve:

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

###

Based on @user2450263 notes above, here are some snippets,

/**
 * Prompt user to enable GPS and Location Services
 * @param mGoogleApiClient
 * @param activity
 */
public static void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    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. 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(
                                activity, 1000);
                    } 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 use it like,

mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .enableAutoManage(this, 34992, this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    locationChecker(mGoogleApiClient, MyActivity.this);

Basically your user will get a prompt like this, where they can enable Location in High Accuracy mode without going in the settings
Turn on GPS

###

I have solution, tested on Android 6.0.1 Marshmallow, on button event OnClick call the void:

private void openGPSSettings() {
    //Get GPS now state (open or closed)
    boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);

    if (gpsEnabled) {
        Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 0);
    } else {
        Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_MODE, 3);
    }
}

Android needs to have USB debugging enabled or be rooted. Execute the command below in adb shell (from a root shell on the device or from a computer connected by USB if the device is not rooted):

pm grant com.example.application.test android.permission.WRITE_SECURE_SETTINGS

Add these permissions in app manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

Leave a Reply

Your email address will not be published. Required fields are marked *