How to enable device administrator for specific apps using Tasker? I would like to make a profile in Tasker to enable device administrator privilegies to "Android Device Manager" when a text message is received. This way I could keep Device Manager disabled, while I can re-enable it remotely (by a text message for example) in case I lost my phone.

Is there any way to do that using Tasker?
Not sure why would you want to do that but you're gonna need root access for sure if you want to enable Android Device Manager as Device administrator in background.

I tried combinations of intent broadcasts to avoid device reboot but with no success, so note that my method requires a reboot. 

All the enabled Device administrators are noted in the file /data/system/device_policies.xml. Simply add or remove an app's entries from it, reboot the device and that app would get enabled/disabled as Device administrator.

Any app which shows up under Settings &rarr; Security &rarr; Device administrator has a receiver meant specifically to meet the requirements of using the Device Administration API and requires the permission BIND_DEVICE_ADMIN. You can check the receiver's name in the Manifest of the app. It is this receiver which is mentioned in the device_policies.xml file.

The simplest way to get the app's device admin receiver's name and the flag is to enable the app as Device administrator, look for the app's entries under device_policies.xml file and then revoke the Device administrator privilege . 

For Android Device Manager (ADM; managed by Google Play Services), the entries are:

&lt;admin name="com.google.android.gms/com.google.android.gms.mdm.receivers.MdmDeviceAdminReceiver"&gt;
&lt;policies flags="28" /&gt;
&lt;/admin&gt;


Now, let's make a task to add those entries in the said XML file whenever needed.

Note: First, Busybox is required for the task, but you can do away its need and use Tasker's inbuilt functionality for sorting if you look closely at the action I proposed to enable ADM. Second, my solution is tested on Android 5.0.2 and 5.1.1 but it should work on other Android releases as well with little tinkering.


Create a task named "Enable ADM".
Create the following actions in that task:


Code &rarr; Run Shell,


Command: 

if [ "$(grep -o '&lt;/policies&gt;' /data/system/device_policies.xml)" != "&lt;/policies&gt;" ]; then sed -i 's/&lt;policies setup-complete="true" \/&gt;/&lt;policies setup-complete="true" \/&gt;\n&lt;admin name="com.google.android.gms\/com.google.android.gms.mdm.receivers.MdmDeviceAdminReceiver"&gt;\n&lt;policies flags="28" \/&gt;\n&lt;\/admin&gt;\n&lt;\/policies&gt;/g' /data/system/device_policies.xml; fi; if [ "$(grep -o 'com.google.android.gms/' /data/system/device_policies.xml)" != "com.google.android.gms/" ]; then sed -i 's/&lt;\/policies&gt;/&lt;admin name="com.google.android.gms\/com.google.android.gms.mdm.receivers.MdmDeviceAdminReceiver"&gt;\n&lt;policies flags="28" \/&gt;\n&lt;\/admin&gt;\n&lt;\/policies&gt;/g' /data/system/device_policies.xml; fi


The command may look a bit incomprehensible at first, but it's plain and simple in reality . The whole command consists of two simple conditions and actions. The first if condition is checking whether &lt;/policies&gt; string is available in the file or not. If any app is enabled as Device administrator then the app's receiver and flag would show up between &lt;policies setup-complete="true"&gt; and &lt;/profiles&gt;. If none of the apps is enabled then &lt;/policies&gt;  would not show up so we can easily append ADM's entries into the file.

The second if condition is checking whether ADM is already enabled or not. If not, then add the entries to enable it. 
Check Use Root

Code &rarr; Run Shell,


Command: setprop ctl.restart zygote

This would soft-reboot the device.
Check Use Root

Instead of soft-reboot, you can opt for normal reboot from System &rarr; Reboot &rarr; Type: Normal

Create an event profile from Event &rarr; Phone &rarr; Received Text and set,


Type: SMS
Sender: Choose a number or leave it blank
Content: Specify what should be in the text to trigger the task such as "Yo boy!". Alternatively, you can avoid this stage and leave everything pretty much global but use an action or more in a task for more complexity as well as flexibility.

Assign the task to the profile.


That's it. You can test your profile now. It should work flawlessly. If it doesn't, then I'm not at fault! Keep troubleshooting since you know the concept now.