HarmonyOS鸿蒙Next中Android10使用Device owner禁用地理位置信息报错报错

HarmonyOS鸿蒙Next中Android10使用Device owner禁用地理位置信息报错报错 现在做了个设备管理应用,已经成功设为 Device Owner(图1、2),其他功能像禁用相机、状态栏、截屏都好好的,唯独关位置这一项死活不行。因为编译的时候符号检查始终要出问题,没办法,我只能用反射DevicePolicyManager.class.getMethod(“setLocationDisabled”, ComponentName.class, boolean.class).invoke(…),但每次一调用就会抛异常,抓到的消息大概就是“android.app.admin.DevicePolicyManager.setLocationDisabled [class android.content.ComponentName, boolean]”,我这台手机是华为的,鸿蒙 4,是安卓 10,有没有哪位在用鸿蒙 4 的设备上,尝试过用 Device Owner 关位置成功的,或者知道有没有别的路子,比如换个方法名或者加个什么权限能过去?已经折腾好久了,求大佬指点,感激不尽。


更多关于HarmonyOS鸿蒙Next中Android10使用Device owner禁用地理位置信息报错报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

devicePolicyManager.setSecureSetting(myDeviceAdmin, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF.toString())

setLocationDisabled 并不存在

更多关于HarmonyOS鸿蒙Next中Android10使用Device owner禁用地理位置信息报错报错的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


Android的基本不研究了,我有Mate40Pro,4.0的,需要的话可以帮忙测试一下

在HarmonyOS NEXT中,Android 10的Device Owner机制已被移除或不再兼容。系统不再支持通过该方式禁用地理位置信息,因为鸿蒙Next已非Android分支,相关API无效。报错源于接口未实现或权限被系统拦截。直接使用鸿蒙原生定位管理接口操作即可。

鸿蒙 4(Android 10)的 DeviceOwner 已限制 setLocationDisabled 反射调用,该 API 被系统屏蔽。替代方案是使用 DevicePolicyManager.setSecureSetting() 直接关闭位置服务,无需反射。

DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName admin = getWho(context); // 你的DeviceAdminReceiver组件
if (dpm.isAdminActive(admin)) {
    // 将位置模式设为 OFF(LOCATION_MODE_OFF = 0)
    dpm.setSecureSetting(admin, Settings.Secure.LOCATION_MODE, "0");
}

若需阻止用户重新开启,可追加用户限制:

dpm.addUserRestriction(admin, UserManager.DISALLOW_SHARE_LOCATION);

setSecureSetting 是 DeviceOwner 的公开 API,无需额外权限,鸿蒙 4 测试可用。

回到顶部