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.
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.
Here is my receiver
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)
}
}