How to transfer ownership(Device owner) from one android application to another?


There can be many ways to achieve transfer ownership(Device owner) from one android application to another. Transfer of ownership means transfer of one administrator to another. I am going to explain one of the simplest ways to transfer ownership in this blog.

In Device owner mode:

Once the device is factory reset and provisioned using QR-code or different technique, it downloads the application A which will provision the device from the download path given in the QR-Code.The application A will get installed on the device and it will start the provisioning process. The application A will get all the provisioning data required from the QR code as ADMIN_EXTRAS. The application A provisions the device and becomes the administrator (device owner). The application A can start downloading an apk file for application B and then install it accordingly. Once Application B is installed on the device, we can make application B as the device owner by transferring ownership using the DevicePolicyManager apis provided by android.

Define a PersistableBundle to store the admin extras received during provisioning process, for example :-


PersistableBundle lObjPersistableBundle = new PersistableBundle(); lObjPersistableBundle.putString(key, value);

lObjPersistableBundle.putString(key, value);

Now from Application A, get the instance of DevicePolicyManager


dpm.transferOwnership(new ComponentName("Application A packagename", "Application A Device Admin Receiver"),new ComponentName("Application B packagename", "Application A Device Admin Receiver"),lObjPersistableBundle);

Transfer the ownership from application A to application B using the following api,


final DevicePolicyManager dpm = (DevicePolicyManager) yourContextObject.getSystemService(Context.DEVICE_POLICY_SERVICE);

For example :-


dpm.transferOwnership(new ComponentName("com.application.a", "com.application.a.aDeviceAdmin"),new ComponentName("com.application.b", "com.application.b.bDeviceAdmin"),lObjPersistableBundle);

lObjPersistableBundle contains the admin extras needed to be transferred to application B.

This operation is atomic,either complete or not done at all. The incoming target administrator must have the <support-transfer-ownership /> tag inside the <device-admin></device-admin> tags in the xml file referenced by DeviceAdminReceiver#Device_Admin_Meta_Data. Otherwise an IllegalArgumentException  will be thrown.

If the operation of transfer of ownership was successful, the data passed during the transfer in persistable bundle will be received in DeviceAdminReceiver#onTransferOwnershipComplete(@NonNull Context context, @Nullable PersistableBundle bundle). Application B will now become the device owner. If operation was unsuccessful, then Application A will remain as device owner.

Leave A Comment

Your email address will not be published. Required fields are marked *