Enable / Disable GPS on Android 4.4.2

Asked

Viewed 3,150 times

1

Hello. I am developing a tracking application where at a given time is made the request of the user’s location activating your GPS and after that, is made the GPS deactivation. In older versions of Android as in 2.3.3 works usually but in the 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);
    }
}

Someone would know how to fix this?

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));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.