Work around for Device admin api does not own profile

这一生的挚爱 提交于 2019-12-04 15:18:58

NFC provisioning is only required for setting a device owner.

I would say the dpm command is generally used also for testing/setting the device owner (that's what I've used it for), but you can also set a profile owner.

You can set up a managed profile programmatically using the code below. The apps that you want to manage must be installed in this profile. This code was based off of android-AppRestrictionEnforcer.

/**
 * Initiates the managed profile provisioning. If we already have a managed profile set up on
 * this device, we will get an error dialog in the following provisioning phase.
 */
private void provisionManagedProfile() {
    Activity activity = getActivity();
    if (null == activity) {
        return;
    }
    Intent intent = new Intent(ACTION_PROVISION_MANAGED_PROFILE);
    if (Build.VERSION.SDK_INT >= 24) {
        intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
                CustomDeviceAdminReceiver.getComponentName(activity));
    } else {
        //noinspection deprecation
        intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
                activity.getApplicationContext().getPackageName());
        intent.putExtra(EXTRA_DEVICE_ADMIN, CustomDeviceAdminReceiver.getComponentName(activity));
    }
    if (intent.resolveActivity(activity.getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_PROVISION_MANAGED_PROFILE);
        activity.finish();
    } else {
        Toast.makeText(activity, "Device provisioning is not enabled. Stopping.",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_PROVISION_MANAGED_PROFILE) {
        if (resultCode == Activity.RESULT_OK) {
            Toast.makeText(getActivity(), "Provisioning done.", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "Provisioning failed.", Toast.LENGTH_SHORT).show();
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

The device owner or profile owner app needs to be set during the setup of the device or work profile. Unlike for device admin, it cannot be changed after setup without factory reset (or removing the work profile). To configure your app to be a device owner or profile owner, see the Android EMM Developers documentation.

It's quite complicated to implement this in your own app, and instead you should consider using the Android Management API which offers the same features as a simpler cloud API. For example the equivalent of setPermissionGrantState is PermissionGrant.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!