androiddevice-admin

How to pass extras to DeviceAdminReceiver?


I use the following code to activate device administrator successfully.

public static void goToActivateDeviceAdmin(Context context, ComponentName admin)
{
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, Html.fromHtml(context.getString(R.string.admin_explain)));
    intent.putExtra("lock", true); // TODO cannot pass custom extras
    context.startActivity(intent);
}

And code for DeviceAdminReceiver:

    public static class AdminReceiver extends DeviceAdminReceiver
{
    @Override
    public void onEnabled(Context context, Intent intent)
    {
        if (intent.getBooleanExtra("lock", false)) // TODO cannot receive extra
            ((DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE)).lockNow();
    }
}

In the receiver, I expect to get true for lock extra, but it's always false. So how can I pass custom extras to DeviceAdminReceiver? Thanks in advance.


Solution

  • In the receiver, I expect to get true for lock extra, but it's always false.

    You are putting that extra on an Intent that starts an activity. That will not be transferred to arbitrary other Intent objects.

    So how can I pass custom extras to DeviceAdminReceiver?

    You can't. You will need to solve your problem in some other way (e.g., SharedPreferences).