Enable / disable GPS on Android 4.4.2

Hello. I am developing a tracking application where at a certain time the request of the user's location is made by activating his GPS and after that, the deactivation of GPS is made. In older versions of Android such as 2.3.3 works normally but in version 4.4.2 no longer works. The code used was:

private void ativarGps() {
    String provider = Settings.Secure.getString(this.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.sendBroadcast(poke);
    }
}

private void desativarGPS() {
    String provider = Settings.Secure.getString(this.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.sendBroadcast(poke);
    }
}

Would Anyone know how to solve this?

Author: rsicarelli, 2015-03-05

1 answers

As of version 2.3.3, you can no longer enable/disable gps without user approval.

All you can do is open the GPS activation screen if necessary:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean GPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

if(!GPSEnabled){
    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
 3
Author: rsicarelli, 2015-03-05 18:55:29