Android:设备管理员:从服务启动设备管理员

时间:2012-05-14 10:00:52

标签: android device-admin

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

我的代码如下......

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);

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

以下是有关如何执行该操作的明确示例(官方文档herehere错过了某些背景信息)

//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);

Here is the reference class used in the official sample app.