12

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");

Thanks in advance!

Miriam
  • 347
  • 1
  • 2
  • 19

2 Answers2

5

You can only set the device owner if there is no user account on the device, or the device is rooted. This normally means you can only set the device owner immediately after a factory reset. You can set the device owner using NFC provisioning (I've never done this) or in an adb shell. The adb method requires the user to enable developer mode, connect the Android device to a computer, and type a command into the shell.

Pseudocode for becoming the device owner:

if the app is not the device owner
  if there is a user account
    inform the user that the app cannot become the device owner
  else if the app is a device administrator
    instruct the user to run the adb command
  else
    request to become a device administrator

My Device Owner app is a complete working example, including how the command that the user must type is generated from the app's package name.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • 1
    But to enable developer mode you must ifrst undergo the initial device setup after factory reset, and part of that setup is specifying a user account that automatically becomes the owner when setup is complete. At least on Android 8.1. – astralmaster Jan 11 '20 at 22:47
  • @astralmaster On 6.x, you can skip setting up a user account. – Kevin Krumwiede Jan 15 '20 at 02:44
  • Affirmative, but since OP did not specify the exact version of Android, I thought it would be better for others to know that in later versions setting device owner using `adb` would be impossible. The correct way would be using NFC or QR code provisioning. – astralmaster Jan 15 '20 at 13:08
4

I've run into this issue recently that I couldn't set device admin via adb, after bit of goofing around I've found a way to do it on Android 9 (it might work on lower versions too). Phone I used was Xiaomi Mi Mix 3

  1. Do factory reset
  2. Gain access to developer options
  3. In developer options turn USB debugging on, at this point You'll be asked to login to user account, do it.
  4. In developer options turn on USB debugging(Security settings)
  5. In developer options turn off Notify about high-risk features
  6. Now in settings go to accounts tab and logout/remove previously added account.
  7. Plug Your phone to PC, grant debugging access
  8. Issue adb shell dpm set-active-admin --user current package_name/device_admin_receiver
  9. and adb shell dpm set-device-owner --user current package_name/device_admin_receiver
RaStall
  • 55
  • 1
  • 4