Frage

I know. Don't do this. I don't care. It's for a root app.

The app is installed to /system/app/ with 0777 permission

I was previously using:

ContentResolver cr = context.getContentResolver();
Settings.Secure.setLocationProviderEnabled(cr, LocationManager.GPS_PROVIDER, !isGpsOn);

This is how I'm trying it on 4.4 since that was deprecated:

int value;
if (isGpsOn)value = Settings.Secure.LOCATION_MODE_OFF;
        else value = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
Settings.Secure.putInt(cr, Settings.Secure.LOCATION_MODE, value);

And it is silently failing (according to some user reports). How can I properly toggle GPS with Android 4.4 from an app in System folder?

War es hilfreich?

Lösung

Don't do this. :)

Check the code for GPS TOGGLER

GPS toggle widget for Android rooted devices. It wrks well even for those ROMs and kernels, other software failed.

With your code, just checking:
Hope you declared the following permissions in your manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

I had used this on rooted LG Optimus, android 4.0/4.1. Reportedly this may no longer work but i am not sure about it for all devices and o.s.:

Switch it ON/OFF with a flag check and this code:

try {  
  Settings.Secure.putString (context.getContentResolver(),  
  Settings.Secure.LOCATION_PROVIDERS_ALLOWED, String.format ("%s,%s",  
  Settings.Secure.getString (context.getContentResolver(),  
  Settings.Secure.LOCATION_PROVIDERS_ALLOWED), LocationManager.GPS_PROVIDER));  
        } catch(Exception e) {}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top