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 → Security → 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
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:
<policies flags="28" />
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 → Run Shell,
if [ "$(grep -o '</policies>' /data/system/device_policies.xml)" != "</policies>" ]; then sed -i 's/<policies setup-complete="true" \/>/<policies setup-complete="true" \/>\n<admin name="com.google.android.gms\/com.google.android.gms.mdm.receivers.MdmDeviceAdminReceiver">\n<policies flags="28" \/>\n<\/admin>\n<\/policies>/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/<\/policies>/<admin name="com.google.android.gms\/com.google.android.gms.mdm.receivers.MdmDeviceAdminReceiver">\n<policies flags="28" \/>\n<\/admin>\n<\/policies>/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
</policies> 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
<policies setup-complete="true"> and
</profiles>. If none of the apps is enabled then
</policies> 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 → Run Shell,
- Instead of soft-reboot, you can opt for normal reboot from System → Reboot → Type: Normal
Create an event profile from Event → Phone → 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.