1

I am trying to enable/ disable GPS. I have tried this code-:

 //Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);

It gives Security permission Error. I have also tried to open settings-:

Intent in = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);   

startActivity(in);

It does not work on every device. Is their any solution to deal with all version of android.

Kaifi Girdhar
  • 471
  • 5
  • 17
  • 1
    Please check my answer here [Android device GPS on/off programatically](http://stackoverflow.com/a/22529296/3330969). Check 4th point. – Lucifer Aug 21 '15 at 07:02
  • I think that you cannot enable and disable it directly, you need to show a dialog where the user decide –  Aug 21 '15 at 07:03

1 Answers1

4

This is deprecated. You can't switch on/off location programmatically. Better I suggest you to use the latest version of GooglePlayService. Using PlayService you can check at anytime whether the location is enabled or not. If it's not enabled then the AlertDialog to enable it. Without even leaving your application you can enable GPS. If it's not enabled then you can do whatever the operation you want to perform. You can find the more details from this post

Basically your Activity should implement ResultCallback<LocationSettingsResult>

And register PendingIntent like below,

PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(
                    mGoogleApiClient,
                    mLocationSettingsRequest
            );
    result.setResultCallback(this);

Now you have a onResult callback in that you can perform what you want to do

@Override
public void onResult(LocationSettingsResult locationSettingsResult) {
    final Status status = locationSettingsResult.getStatus();
    switch (status.getStatusCode()) {
        case LocationSettingsStatusCodes.SUCCESS:
            Log.i(TAG, "All location settings are satisfied.");
            startLocationUpdates();
            break;
        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
            Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                    "upgrade location settings ");
            try {
                // Show the dialog by calling startResolutionForResult(), and check the result
                // in onActivityResult().
                status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                Log.i(TAG, "PendingIntent unable to execute request.");
            }
            break;
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                    "not created.");
            break;
    }
}
// This code is from https://github.com/googlesamples/android-play-location/blob/master/LocationSettings/app/src/main/java/com/google/android/gms/location/sample/locationsettings/MainActivity.java

Please find the sample google project in Github

Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36