简体   繁体   English

如何让我的应用成为设备所有者?

[英]How to make my app a device owner?

The device policy manager API docs and the android 5.0 overview both mention something about a device owner app . 设备策略管理器API文档Android 5.0概述都提到了有关设备所有者应用程序的内容 How can I setup my app as a device owner ? 如何将我的应用设置为设备所有者

Edit: Is there any other ways than rooting and NFC if available please share. 编辑:除了root和NFC之外还有其他方法吗?请分享。

There's actually a way other than NFC and rooting to set an application as a device owner app. 实际上除了NFC和root之外还有一种方法可以将应用程序设置为设备所有者应用程序。 You could use the dpm command line tool from an adb shell . 您可以使用adb shelldpm命令行工具。

Usage : 用法:

usage: dpm [subcommand] [options]
usage: dpm set-device-owner <COMPONENT>
usage: dpm set-profile-owner <COMPONENT> <USER_ID>

dpm set-device-owner: Sets the given component as active admin, and its package as device owner.
dpm set-profile-owner: Sets the given component as active admin and profile owner for an existing user.

UPDATE : The dpm utility is really simple actually. 更新: dpm实用程序实际上非常简单。 Its goal is to create a new file called device_owner.xml under /data/system/device_owner.xml that references the Device/Profile owner apps. 它的目标是创建一个名为新文件device_owner.xml/data/system/device_owner.xml引用设备/配置文件所有者的应用程序。

The Android platform is then reading this file to check which application is considered as a Device Owner or Profile Owner App . Android平台随后将读取此文件以检查哪个应用程序被视为设备所有者配置文件所有者应用程序

On a rooted device , you could indeed create this file by yourself , but since the dpm tool is doing it, you'd better use it (DRY principle) : 在root设备上 ,您确实可以自己创建此文件 ,但由于dpm工具正在执行此操作,因此您最好使用它(DRY原则):

For example via a Runtime.exec() command: 例如,通过Runtime.exec()命令:

Runtime.getRuntime().exec("dpm set-device-owner com.foo.deviceowner/.DeviceAdminRcvr");

Also notice that this tool is working only if no account is set for the user (make sure no account is set in Settings > Accounts ) before its use. 另请注意,只有在用户未设置帐户(确保在设置>帐户中未设置帐户 )之前,此工具才有效。

Source and more information at Android shell command tool : Device Policy Manager Android shell命令工具中的 源和更多信息 :设备策略管理器

If you're root on your device, you can follow this method to become device owner. 如果您是设备上的root用户,则可以按照此方法成为设备所有者。

First, create a file device_owner.xml with following content: 首先,创建一个包含以下内容的文件device_owner.xml

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<device-owner package="your.owner.app.package.id" name="Your app name" />

Now do the following steps 现在执行以下步骤

  1. adb push device_owner.xml /sdcard/

  2. adb shell

  3. su

  4. cp /sdcard/device_owner.xml /data/system/

  5. cd /data/system/

  6. chown system:system device_owner.xml

  7. reboot 重启

Note : Before rebooting device, make sure that you installed the application, which you are trying to make device owner. 注意:在重新启动设备之前,请确保已安装了您尝试设备所有者的应用程序。 If you will not do, you will get boot animation for infinite time. 如果你不这样做,你将获得无限时间的启动动画。

Update: 更新:

On my Android 7.1.2 set-top box (AOSF and rooted), I found a couple things that have evolved over time. 在我的Android 7.1.2机顶盒(AOSF和rooted)上,我发现了一些随着时间的推移而发展的东西。

  1. exec("dpm set-device-owner ...") throws and exception unless <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" /> is declared in the AndroidManifest.xml. exec("dpm set-device-owner ...")抛出异常,除非在AndroidManifest.xml中声明<uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" /> But that brings other issues, more about that here . 但这带来了其他问题,更多关于此问题
  2. The file /data/system/device_policy.xml doesn't appear anymore. 文件/data/system/device_policy.xml不再出现。 Instead, it's now /data/system/device_policy_2.xml and the schema is slightly different. 相反,它现在是/data/system/device_policy_2.xml ,架构略有不同。 Running dpm set-device-owner com.myDomain.myPackage/.myComponent through an adb shell generates the file as: 通过adb shell运行dpm set-device-owner com.myDomain.myPackage/.myComponent生成以下文件:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<root>
    <device-owner package="com.myDomain.myPackage" name="" component="com.myDomain.myPackage/com.myDomain.myPackage.myComponent" userRestrictionsMigrated="true" />
</root>

You can also use reflexivity, by calling the DevicePolicyManager method called setProfileOwner which was hidden in the SDK Documentation. 您还可以通过调用SDK文档中隐藏的名为setProfileOwnerDevicePolicyManager方法来使用自反性。

Don't forget to cancel it otherwise you'll have some conflicts with the Google Play ;) 不要忘记取消它,否则你会与Google Play发生冲突;)

Just tried, and the dpm command requires root privilege on real devices(Samsung T550 for example), otherwise it will fail with SecurityException . 刚试过, dpm命令需要在真实设备上使用root权限(例如Samsung T550),否则它将因SecurityException而失败。 adb shell only grants root on android emulators. adb shell只在Android模拟器上授予root权限。 So you will have to root the device first. 所以你必须首先根设备。

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

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