Enable / Disable GPS on Android 4.4.2

1

Hello. I'm developing a tracking application where at a certain point the user's location request is activated by activating their GPS and after that, GPS deactivation is done. In older versions of Android as in 2.3.3 it works but usually 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?

    
asked by anonymous 05.03.2015 / 19:25

1 answer

3

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));
}
    
05.03.2015 / 19:55