简体   繁体   English

在设备所有者应用中启用GPS

[英]Enable GPS in a device owner app

According API documentation a device owner app can modify a few "secure settings" and specially the LOCATION_MODE with the following call : 根据API文档 ,设备所有者应用可以通过以下调用来修改一些“安全设置”,尤其是LOCATION_MODE

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

Called by profile or device owners to update Settings.Secure settings [...] 个人资料或设备所有者致电以更新Settings.Secure设置[...]

A device owner can additionally update the following settings: LOCATION_MODE 设备所有者可以另外更新以下设置: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). 根据我的理解,LOCATION_MODE的值是一个整数(分别为0(禁用位置),1(仅用于GPS),2(用于省电模式)和3(用于高精度)。

My problem is the type of the String value parameter. 我的问题是String value参数的类型。 LOCATION_MODE requires an int, but the API requires a String. LOCATION_MODE需要一个int,但API需要一个String。

Did I miss something ? 我错过了什么 ?

Solution is simply to use the String representation of the int value. 解决方案是简单地使用int值的String表示形式。

For instance to enable "gps only" location mode : 例如,启用“仅GPS”定位模式:

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] [感谢@Selvin评论]

It make sense, because when digging into javadoc for LOCATION_MODE , you can read : 这是有道理的,因为在为LOCATION_MODE挖掘javadoc时,您可以阅读:

Note that internally setting values are always stored as strings[...] 请注意,内部设置值始终存储为字符串[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM