I’m aware of the command "adb shell dpm set-device-owner pacakgename"
I want to know if there’s a way to programmatically ask and set device owner for one app after it’s launched
basically the opposite of :
DevicePolicyManager.clearDeviceOwnerApp("com.package.something");
To programmatically set a package as the device owner, you need to use the Android Device Policy API. The following code shows how to do it programmatically:
In the code above, you need to create a
DeviceAdminReceiver
class and override theonEnabled
method. In theonEnabled
method, you can use theDevicePolicyManager
class to set the device owner.After creating the class, you need to declare it in the
AndroidManifest.xml
file:The
device_admin_receiver.xml
file should look like this:Once the
DeviceOwnerReceiver
class is declared and the device owner is set, your app can run as the device owner.