4

我无法获得允许用户授予应用程序成为设备管理员工作权限的活动。

我的代码如下...

ComponentName comp = new ComponentName(this, CustomReceiver.class);

Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

i.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, comp);
i.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Explanation");

startActivity(i);

应用程序不会崩溃/报告异常。我可能做错了什么?

4

2 回答 2

1

这样的事情会做

if (!mPolicy.isAdminActive()) {

    Intent activateDeviceAdminIntent =
        new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

    activateDeviceAdminIntent.putExtra(
        DevicePolicyManager.EXTRA_DEVICE_ADMIN,
        mPolicy.getPolicyAdmin());

    // It is good practice to include the optional explanation text to
    // explain to user why the application is requesting to be a device
    // administrator. The system will display this message on the activation
    // screen.
    activateDeviceAdminIntent.putExtra(
        DevicePolicyManager.EXTRA_ADD_EXPLANATION,
        getResources().getString(R.string.device_admin_activation_message));

    startActivityForResult(activateDeviceAdminIntent,
        REQ_ACTIVATE_DEVICE_ADMIN);
}

可能你没有考虑

mPolicy.getPolicyAdmin()
于 2012-05-14T10:05:25.143 回答
1

这是一个关于如何做到这一点的明确示例,(此处此处的官方文档缺少一些上下文)

//class that implements DeviceAdminReceiver, defined in the Manifest 
ComponentName deviceAdminCN = new ComponentName(context, DeviceAdminReceiverImpl.class) 

...

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminCN);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "your explanation for the user here");
startActivityForResult(intent, YOUR_REQUEST_CODE);

这是官方示例应用程序中使用的参考类。

于 2016-09-20T16:06:56.563 回答