0

I am trying to create an app for my own use that enables/disables certain features like WiFi, GPS, etc; my question pertains to GPS. Note that my phone is not rooted and I would like to keep it that way.

I have successfully enabled GPS using the following code:

public void toggleGPS() {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

    LocationRequest locationRequest = new LocationRequest();

    if (isGPSEnabled()) {
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(60 * 60 * 1000);
        locationRequest.setFastestInterval(60 * 60 * 1000);
    } else {
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(1000);
        locationRequest.setFastestInterval(1000);
    }

    googleApiClient.connect();

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

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

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            Status status = result.getStatus();
            LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();

            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {

                }
            }
        }
    });
}

So when GPS isn't on, this code prompts me to turn it on, as I want. However, it doesn't seem to shut off when I request Balanced Power and set the larger intervals and no other app is using GPS. I assumed that if my app no longer needs high accuracy, then GPS will automatically be switched off. Am I misunderstanding something? And is there any way to switch it off? I can sort of understand the security concerns for disallowing GPS to be ENABLED programmatically, but I don't understand why Android wouldn't allow it to be DISABLED.

Thanks in advance!

user1422348
  • 345
  • 1
  • 7
  • 23
  • hey follow these link for your answer [http://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android](http://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android) – sud Nov 23 '15 at 06:43
  • the andorid will not allow Enable as well as disable gps progamatically above android 4.0, because of security concern... – sud Nov 23 '15 at 06:48
  • Thanks, but the bug that allowed that exploit has already been patched. – user1422348 Nov 23 '15 at 06:55

1 Answers1

0

try this code it works on all versions, make a try ....

// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider.contains("gps")){ //if gps is enabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        this.ctx.sendBroadcast(poke);
    }
}

and code to enable is as follow you can use this..

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(!provider.contains("gps")){ //if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3")); 
        this.ctx.sendBroadcast(poke);


    }
}
sud
  • 505
  • 1
  • 4
  • 12