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

2024-04-02

根据API文档 https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#setSecureSetting(android.content.ComponentName,%20java.lang.String,%20java.lang.String)设备所有者应用程序可以修改一些“安全设置”,特别是LOCATION_MODE https://developer.android.com/reference/android/provider/Settings.Secure.html#LOCATION_MODE通过以下调用:

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

由配置文件或设备所有者调用以更新设置。安全设置 [...]

设备所有者还可以更新以下设置: LOCATION_MODE

根据我的理解,LOCATION_MODE 的值是一个 int (分别为 0 表示禁用位置,1 表示仅 GPS,2 表示省电模式,3 表示高精度)。

我的问题是类型String value范围。 LOCATION_MODE 需要一个 int,但 API 需要一个 String。

我错过了什么 ?


解决方案很简单,使用 int 值的字符串表示形式。

例如启用“仅 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 评论]

这是有道理的,因为当深入研究 javadoc 时LOCATION_MODE https://developer.android.com/reference/android/provider/Settings.Secure.html#putInt(android.content.ContentResolver,%20java.lang.String,%20int), 你可以阅读 :

请注意,内部设置值始终存储为字符串[...]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在设备所有者应用程序中启用 GPS 的相关文章

随机推荐