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.
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
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
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
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.
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:
<?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.
Don't forget to cancel it otherwise you'll have some conflicts with the Google Play ;)
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