Enable GPS in a device owner app

1.6k views Asked by At

According API documentation a device owner app can modify a few "secure settings" and specially the LOCATION_MODE with the following call :

devicePolicyManager.setSecureSetting (ComponentName admin, 
            String setting, 
            String value)

Called by profile or device owners to update Settings.Secure settings [...]

A device owner can additionally update the following settings: LOCATION_MODE

According my understanding the value of LOCATION_MODE is an int (resp. 0 for location disabled, 1 for GPS only, 2 for battery saving mode and 3 for high accuracy).

My problem is the type of the String value parameter. LOCATION_MODE requires an int, but the API requires a String.

Did I miss something ?

1

There are 1 answers

0
ben75 On BEST ANSWER

Solution is simply to use the String representation of the int value.

For instance to enable "gps only" location mode :

DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
if (dpm.isDeviceOwnerApp(context.getPackageName())) {
    ComponentName componentName = new ComponentName(context, MyDeviceAdmin.class);
    dpm.setSecureSetting(componentName, Settings.Secure.LOCATION_MODE, String.valueOf(Settings.Secure.LOCATION_MODE_SENSORS_ONLY));
}

[Thanks to @Selvin comment]

It make sense, because when digging into javadoc for LOCATION_MODE, you can read :

Note that internally setting values are always stored as strings[...]