简体   繁体   English

以编程方式将包设置为设备所有者

[英]Programmatically set package as device owner

I'm aware of the command "adb shell dpm set-device-owner pacakgename"我知道命令"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.如果设备上没有用户帐户,或者设备已root,您只能设置设备所有者。 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.您可以使用 NFC 配置(我从未这样做过)或在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. adb方法需要用户开启开发者模式,将 Android 设备连接到电脑,然后在 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).我最近遇到了这个问题,我无法通过 adb 设置设备管理员,经过一番折腾之后,我找到了一种在 Android 9 上执行此操作的方法(它也可能适用于较低版本)。 Phone I used was Xiaomi Mi Mix 3我用的手机是小米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.在开发者选项中打开USB调试,此时您将被要求登录用户帐户,执行此操作。
  4. In developer options turn on USB debugging(Security settings)在开发者选项中打开 USB 调试(安全设置)
  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将您的手机连接到 PC,授予调试权限
  8. Issue adb shell dpm set-active-admin --user current package_name/device_admin_receiver发出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_receiveradb 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. 更改它的唯一方法是在设置过程中将设备恢复出厂设置并分配新的软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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