java-在设备所有者应用中启用GPS
作者:互联网
根据API documentation,设备所有者应用可以通过以下调用来修改一些“安全设置”,尤其是LOCATION_MODE:
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
根据我的理解,LOCATION_MODE的值是一个整数(分别为0(禁用位置),1(仅用于GPS),2(用于省电模式)和3(用于高精度).
我的问题是字符串值参数的类型. LOCATION_MODE需要一个int,但API需要一个String.
我错过了什么 ?
解决方法:
解决方案是简单地使用int值的String表示形式.
例如,启用“仅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));
}
[感谢@Selvin评论]
这是有道理的,因为在为LOCATION_MODE挖掘javadoc时,您可以阅读:
Note that internally setting values are always stored as strings[…]
标签:android,java,gps,device-owner 来源: https://codeday.me/bug/20191009/1882144.html