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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21183328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:06:38  来源:igfitidea点击:

How to make my app a device owner?

androiddevice-admin

提问by Vishal Santharam

The device policy manager API docsand the android 5.0 overviewboth mention something about a device owner app. How can I setup my app as a device owner?

装置策略管理器API文档安卓5.0概述提东西都约有设备所有者的应用程序。如何将我的应用设置为设备所有者

Edit: Is there any other ways than rooting and NFC if available please share.

编辑:如果可用,除了生根和 NFC 之外还有其他方法吗,请分享。

回答by Florent Dupont

There's actually a way other than NFC and rooting to set an application as a device owner app. You could use the dpmcommand line tool from an adb shell.

除了 NFC 和生根之外,实际上还有一种方法可以将应用程序设置为设备所有者应用程序。您可以使用.dpm命令行工具adb shell

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 dpmutility is really simple actually. Its goal is to create a new file called device_owner.xmlunder /data/system/device_owner.xmlthat references the Device/Profile ownerapps.

更新:dpm实用程序实际上非常简单。它的目标是创建一个名为device_owner.xmlunder的新文件,/data/system/device_owner.xml该文件引用设备/配置文件所有者应用程序。

The Android platform is then reading this file to check which application is considered as a Device Owneror Profile Owner App.

然后,Android 平台会读取此文件以检查哪个应用程序被视为Device OwnerProfile Owner App

On a rooted device, you could indeed create this file by yourself, but since the dpmtool is doing it, you'd better use it (DRY principle) :

在root设备上,你确实可以自己创建这个文件,但是既然dpm工具在做,你最好使用它(DRY原则):

For examplevia 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.

另请注意,此工具仅在使用前未为用户设置帐户(确保在Settings > Accounts中未设置帐户)时才有效。

Source and more informationat Android shell command tool : Device Policy Manager

来源和更多信息Android的shell命令工具:设备规范管理

回答by Spynet

If you're root on your device, you can follow this method to become device owner.

如果您是设备上的 root,则可以按照此方法成为设备所有者。

First, create a file device_owner.xmlwith 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

  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. 重启

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.

注意:在重新启动设备之前,请确保您安装了您正在尝试让设备拥有者的应用程序。如果您不这样做,您将获得无限时间的启动动画。

回答by Gary Kumfert

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 和 root)上,我发现了一些随着时间的推移而发展的东西。

  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. But that brings other issues, more about that here.
  2. The file /data/system/device_policy.xmldoesn't appear anymore. Instead, it's now /data/system/device_policy_2.xmland the schema is slightly different. Running dpm set-device-owner com.myDomain.myPackage/.myComponentthrough an adb shell generates the file as:
  1. exec("dpm set-device-owner ...")throws 和异常,除非<uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />在 AndroidManifest.xml 中声明。但这带来了其他问题,更多关于这里
  2. 该文件/data/system/device_policy.xml不再出现。相反,现在/data/system/device_policy_2.xml和架构略有不同。dpm set-device-owner com.myDomain.myPackage/.myComponent通过 adb shell运行生成的文件为:
<?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>

回答by Ektos974

You can also use reflexivity, by calling the DevicePolicyManagermethod called setProfileOwnerwhich was hidden in the SDK Documentation.

您还可以通过调用隐藏在 SDK 文档中的DevicePolicyManager方法来使用自反性setProfileOwner

Don't forget to cancel it otherwise you'll have some conflicts with the Google Play ;)

不要忘记取消它,否则您会与 Google Play 发生一些冲突;)

回答by Xavier Lin

Just tried, and the dpmcommand requires root privilege on real devices(Samsung T550 for example), otherwise it will fail with SecurityException. adb shellonly grants root on android emulators. So you will have to root the device first.

刚试过,该dpm命令需要真实设备上的root权限(例如三星T550),否则会失败并显示SecurityException. adb shell仅在 android 模拟器上授予 root 权限。所以你必须先root设备。