0

Runtime.getRuntime().exec("dpm set-device-owner com.integral.kiosk.kioskapp/.receivers.MyDeviceAdminReceiver");

没有在 Android 模拟器 Nexus 6 OS Android 8 中设置设备所有者。但是当使用 adb shell 命令时它工作正常。

附上我在 oncreate 函数中使用的代码。

 ComponentName deviceAdmin = new ComponentName(this, MyDeviceAdminReceiver.class);
        if (!mDevicePolicyManager.isAdminActive(deviceAdmin)) {
            Toast.makeText(this, "Not Device Admin", Toast.LENGTH_SHORT).show();

            Intent activateDeviceAdmin = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            activateDeviceAdmin.putExtra(
                    DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin
            );
            activateDeviceAdmin
                    .putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "After activating admin, you will be able to block application uninstallation.”");
            startActivityForResult(activateDeviceAdmin,
                    PM_ACTIVATION_REQUEST_CODE);
        }


        if (mDevicePolicyManager.isDeviceOwnerApp(getPackageName())) {
            mDevicePolicyManager.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
            Toast.makeText(this, "Device Owner", Toast.LENGTH_SHORT).show();
//            startLockTask();
            mDevicePolicyManager.clearDeviceOwnerApp(getPackageName());
        } else {

            Toast.makeText(this, "Not Device Owner", Toast.LENGTH_SHORT).show();
            try {
                Runtime.getRuntime().exec("dpm set-device-owner com.integral.kiosk.kioskapp/.receivers.MyDeviceAdminReceiver");
            } catch (IOException e) {
                e.printStackTrace();
            }

请帮忙。提前致谢

4

1 回答 1

0

尝试删除所有“添加设备管理员”代码,即第一个 if 语句块中的所有内容。自 Lollipop 以来的每个 Android 版本都弃用了越来越多的设备管理员 API。您的应用只需成为设备所有者。一旦它成为设备所有者,就无法卸载它,您可以从中设置锁定任务,例如它自己。

于 2017-12-08T06:31:03.170 回答