有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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的值为int(分别为0表示位置禁用,1表示仅GPS,2表示电池节省模式,3表示高精度)

我的问题是String value参数的类型。LOCATION_模式需要int,但API需要字符串

我错过什么了吗


共 (1) 个答案

  1. # 1 楼答案

    解决方案只是使用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 comment]

    这是有道理的,因为在深入研究LOCATION_MODE的javadoc时,您可以阅读:

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