Tech Blog

Coding

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

By Kumar gaurav2 min read
← Back to blog

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.

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →