0

I am building a Lost Device Finder app and want to be able to turn on GPS programmatically. I am able to get Modify System Settings permission, but when I try to turn on GPS, Permission Denied exception is thrown.

The following is my Code:-


        if (!Settings.System.canWrite(getBaseContext())) {
            startActivityForResult(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS).setData(Uri.parse(String.format("package:%s", getPackageName()))), request_code);
        }
        else{
            turnGpsOn(getBaseContext());
        }


private void turnGpsOn (Context context) {
        String beforeEnable = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        String newSet = String.format ("%s,%s",
                beforeEnable,
                LocationManager.GPS_PROVIDER);
        try {
            Settings.Secure.putString (context.getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                    newSet);
        } catch(Exception e) {}
    }

And I have defined permissions in Manifest like this,

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

My device is not rooted but I think it can be done as the "PowerShade" app requires Modify System Settings Permission in order to do it.

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
saksham
  • 81
  • 1
  • 7
  • Does this answer your question? [How can I enable or disable the GPS programmatically on Android?](https://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android) – FarshidABZ Jun 17 '20 at 08:15
  • I have already seen this Question. The answers all suggest either displaying a popup or rooting the device. One answer suggests exploiting a bug but The app "PowerShade" works on every device not only the ones with the bug – saksham Jun 17 '20 at 08:36

0 Answers0