简体   繁体   中英

Programmatically set package as device owner

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!

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.

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

Device owner can only be set during a new device setup ie during initial setup of the device. It cannot be changed to another app once the device setup is complete.

The only way to change it would be to factory reset the device and assign the new package during setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM