DevicePolicyManager throws SecurityException

868 Views Asked by At

I am creating an application using Samsung Knox and Device Administration. I am trying to disable the keyguard but I keep getting a SecurityException with the message:

Admin ComponentInfo{com.samsung.safedrive/com.samsung.safedrive.data.receiver.AdminReceiver} does not own the profile

Request admin rights

        override fun requestAdminRights() {
        val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
        val deviceAdminReceiver = ComponentName(this, AdminReceiver::class.java)
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminReceiver)
        intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, packageName)
        startActivityForResult(intent, ADMIN_RIGHTS_REQUEST_CODE)
    }

uses-policies

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <disable-keyguard-features />
        <force-lock />
    </uses-policies>
</device-admin>

DeviceAdminReceiver

class AdminReceiver: DeviceAdminReceiver() {

    override fun onEnabled(context: Context, intent: Intent) {
        super.onEnabled(context, intent)

        val devicePolicyManager = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        val componentName = ComponentName(context, AdminReceiver::class.java)
        devicePolicyManager.setKeyguardDisabled(componentName, true)
    }

    override fun onDisabled(context: Context, intent: Intent) {
        super.onDisabled(context, intent)
    }
}

Manifest

<receiver
            android:name=".data.receiver.AdminReceiver"
            android:description="@string/enterprise_device_admin_description"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin_receiver" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>
***
0

There are 0 best solutions below