Enable GPS in a device owner app

最后都变了- 提交于 2019-12-01 09:55:34

问题


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:


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[...]



来源:https://stackoverflow.com/questions/41286821/enable-gps-in-a-device-owner-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!