9

Is it possible to activate Device Administrator via ADB command instead of tapping

"Setting -> Security -> Device Administrators --> Select App --> Activate"

on handheld?

If it's possible, how?

6

It's not possible. The settings code is specifically written to prevent this. The closest you can come is to bring up the Device administration settings page in the Settings app. You can do this with:

adb shell am start -S "'com.android.settings/.Settings\$DeviceAdminSettingsActivity'"
  • Thanks Dan, Little correction : it should be adb shell am start -S "com.android.settings/.Settings\$DeviceAdminSettingsActivity" Without single quote – marengga May 21 '13 at 4:29
  • In most shells, that command will just start Settings, not the Device administration settings page. Here's Izzy's explanation of why. – Dan Hulme May 21 '13 at 9:26
  • What about disabling? is that possible? – android developer Apr 24 '14 at 21:40
5

Tested and working on Android 7.0 without root:

adb shell
dpm set-active-admin --user current com.company.foo.bar.package/.the.Admin.Reciever

To find the admin receiver of an installed package, use the following to adb shell command and review the output:

adb shell
pm dump com.company.foo.bar.package | grep ' filter' | cut -d ' ' -f 12 | sort | uniq

To give a real world example, here is the command used to activate IBM's Maas360 Android client as a device admin:

adb shell
pm dump com.fiberlink.maas360.android.control | grep ' filter' | cut -d ' ' -f 12 | sort | uniq

Output:
…
com.fiberlink.maas360.android.control/.receivers.GoogleCampaignReceiver
com.fiberlink.maas360.android.control/.receivers.LocalEventReceiver
com.fiberlink.maas360.android.control/.receivers.Maas360DeviceAdminReceiver  <-- This is the one I want
com.fiberlink.maas360.android.control/.receivers.Maas360SecondaryDeviceAdminReceiver
…


Set Device Admin:    
dpm set-active-admin --user current com.fiberlink.maas360.android.control/.receivers.Maas360DeviceAdminReceiver
  • IT WORKS! This must be the accepted answer! I have one question, is it possible to add an extra to that command? I need to request the Samsung Knox license activation right after the administrator activation and I would like to pass the license's key as an extra (so I do not have to store it in my code or on my device) – Cliff Burton Jul 8 at 8:33
1

Yes, provided that you've root access. You would have to add the app's receiver and the policy flag in the file /data/system/device_policies.xml. For example, to enable Tasker as Device administrator add the following lines in the said file,

For Android 5.x: Remove the last line with </policies> if there is already a Device Administrator enabled in the system. Simply add the lines in the file after the line with string <policies setup-complete="true">.

<admin name="net.dinglisch.android.taskerm/net.dinglisch.android.taskerm.MyDeviceAdminReceiver">
<policies flags="8" />
</admin>
</policies>

For Android 4.2.1: Remove the last line with </policies> if there is already a Device Administrator enabled in the system. Simply add the lines in the file after the line with string <policies>.

<admin name="net.dinglisch.android.taskerm/net.dinglisch.android.taskerm.MyDeviceAdminReceiver">
<policies flags="8" />
</admin>
</policies>

Reboot the device for the changes to take effect.

(Busybox required) You can use sed or echo or any command that you're comfortable with to write the file with those lines. For any help, see my answer here: How to enable device administrator for specific apps using Tasker?

0

This is possible. You can use android's new tool UI-Automator to click and interact with any view or button on the system including hitting "Activate" for device admin. Here's how:

  1. Get UI-Automator running. (go to "Configure your development environment" and setup a new java project).
  2. Write some code to interact with your preferences list and click "Activate". Example:

    UiScrollable settingsItem = new UiScrollable(new UiSelector().className("android.widget.ListView")); UiObject listButton = settingsItem.getChildByText(new UiSelector().className("android.widget.LinearLayout"), "Enable Device Admin"); listButton.click(); (new UiObject(new UiSelector().text("Activate"))).clickAndWaitForNewWindow(); me.celebrateWith(new Beer());

  3. Compile it: ant build

  4. Push the jar file: adb -d push bin/LookoutTest.jar /data/local/tmp/
  5. Launch your settings activity: adb shell am start -S "'com.android.settings/.Settings\$DeviceAdminSettingsActivity'"
  6. Run the automation: adb -d shell uiautomator runtest LookoutTest.jar -c DALaunch
  7. Party.
  • Will this work even if the current locale of the device isn't English? – android developer Apr 24 '14 at 21:39
0

you can set the Device as owner from Android Studio. First go to the adb location which is at Platform-tools in Android Sdk and then run the adb shell command. I have give the full path and you can adjust at your requirement upon changing adb path and package name

C:\Users\Owner\AppData\Local\Android\Sdk\platform-tools>adb shell dpm set-device-owner package-name/.MyDeviceAdminReceiver

MyDeviceAdminReceiver is the interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.