How to make my app a device owner?

泪湿孤枕 提交于 2019-11-26 01:58:04

问题


The device policy manager API docs and the android 5.0 overview both mention something about a device owner app. How can I setup my app as a device owner?

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


回答1:


There's actually a way other than NFC and rooting to set an application as a device owner app. You could use the dpm command line tool from an 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 owner apps.

The Android platform is then reading this file to check which application is considered as a Device Owner or 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) :

For example via a Runtime.exec() command:

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




回答2:


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

First, create a file device_owner.xml with following content:

<?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.




回答3:


Update:

On my Android 7.1.2 set-top box (AOSF and rooted), I found a couple things that have evolved over time.

  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.xml doesn't appear anymore. Instead, it's now /data/system/device_policy_2.xml and the schema is slightly different. Running dpm set-device-owner com.myDomain.myPackage/.myComponent through an adb shell generates the file as:
<?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>



回答4:


You can also use reflexivity, by calling the DevicePolicyManager method called setProfileOwner which was hidden in the SDK Documentation.

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




回答5:


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.



来源:https://stackoverflow.com/questions/21183328/how-to-make-my-app-a-device-owner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!