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
How to make my app a device owner?
提问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 dpm
utility is really simple actually. Its goal is to create a new file called device_owner.xml
under /data/system/device_owner.xml
that references the Device/Profile ownerapps.
更新:该dpm
实用程序实际上非常简单。它的目标是创建一个名为device_owner.xml
under的新文件,/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 Owner或Profile Owner App。
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 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.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
现在执行以下步骤
adb push device_owner.xml /sdcard/
adb shell
su
cp /sdcard/device_owner.xml /data/system/
cd /data/system/
chown system:system device_owner.xml
reboot
adb push device_owner.xml /sdcard/
adb shell
su
cp /sdcard/device_owner.xml /data/system/
cd /data/system/
chown system:system device_owner.xml
重启
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)上,我发现了一些随着时间的推移而发展的东西。
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.- The file
/data/system/device_policy.xml
doesn't appear anymore. Instead, it's now/data/system/device_policy_2.xml
and the schema is slightly different. Runningdpm set-device-owner com.myDomain.myPackage/.myComponent
through an adb shell generates the file as:
exec("dpm set-device-owner ...")
throws 和异常,除非<uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
在 AndroidManifest.xml 中声明。但这带来了其他问题,更多关于这里。- 该文件
/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 DevicePolicyManager
method called setProfileOwner
which 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 dpm
command requires root privilege on real devices(Samsung T550 for example), otherwise it will fail with SecurityException
. adb shell
only grants root on android emulators. So you will have to root the device first.
刚试过,该dpm
命令需要真实设备上的root权限(例如三星T550),否则会失败并显示SecurityException
. adb shell
仅在 android 模拟器上授予 root 权限。所以你必须先root设备。