Programmatically set package as device owner
Asked Answered
N

2

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!

Nominate answered 3/5, 2018 at 16:26 Comment(2)
Have you find a solution for this?Cleo
Check this solution, I hope , It will help you softans.com/question/…Anhinga
D
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.

Depot answered 14/5, 2018 at 6:4 Comment(3)
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.Tong
@Tong On 6.x, you can skip setting up a user account.Depot
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.Tong
B
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
Brandwein answered 15/1, 2021 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.