解决设备管理api不拥有配置文件的情况 [英] Work around for Device admin api does not own profile

查看:392
本文介绍了解决设备管理api不拥有配置文件的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过设备管理API进行工作,并在获得的DevicePolicyManager上调用setPermissionGrantState函数时

I was working my way through the device admin api, and while invoking the setPermissionGrantState function on the DevicePolicyManager I got

Unable to start receiver com.xx.admin.receivers.AdminReceiver: java.lang.SecurityException: Admin ComponentInfo{com.xx/com.xx.admin.receivers.AdminReceiver} does not own the profile.

我了解到某些功能只能由设备/配置文件所有者运行.更进一步,NFC provisioning and dpm command是通过它的方式.但这并不是我分发应用程序时要继续进行的方式.有什么方法可以通过要求用户允许我的应用程序具有(或不具有)root用户身份拥有个人资料所有权来自动执行此授权.

I understand that there are certain functions that can be only run by device/profile owners. Further more that NFC provisioning and dpm command is the way through it. But this is hardly the way I want to proceed when I distribute my app. Is there any way I can automate this authorization by requesting the user to permit my app with a profile ownership with/without root.

这是我的接收者

class AdminReceiver : DeviceAdminReceiver() {

    var manager: DevicePolicyManager? = null
    override fun onEnabled(context: Context?, intent: Intent?) {
        super.onEnabled(context, intent)
        manager = getManager(context)
        manager!!.setPermissionGrantState(getComponentName(context!!)
                , "com.abc.app"
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED)


    }

    fun getComponentName(context: Context): ComponentName {
        return ComponentName(context.applicationContext, AdminReceiver::class.java)
    }

}

推荐答案

仅在设置设备所有者时才需要NFC设置.

NFC provisioning is only required for setting a device owner.

我想说dpm命令通常也用于测试/设置设备所有者(这就是我使用的目的),但是您也可以设置配置文件所有者.

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.

您可以使用以下代码以编程方式设置托管配置文件.您要管理的应用程序必须已安装在此配置文件中.该代码基于android-AppRestrictionEnforcer.

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

这篇关于解决设备管理api不拥有配置文件的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆