Enabling Device Administration APIs in Android
In android, Admin level features are like accessing the device password, enabling/disabling the device camera, erasing all the device data, limiting the maximum number of password attempt etc. But these admin features cannot be accessed directly in an android app. In order to use these features, we have to enable the Device Admin APIs.
For using Device Administration APIs in android app, firstly we have to register these APIs and then we can access the admin level functionality.
Steps for Registering the APIs:-
1. Create a sub-class of DeviceAdminReceiver.
2. Register it in the manifest along with the BIND_DEVICE_ADMIN permission and the intent filter with the ability to respond back the ACTION_DEVICE_ADMIN_ENABLED and a declaration of security policies used in metadata as shown below:-
3. The security policies are declared in the xml file device_admin_uses_policies.xml as shown below:-
It will be stored in the xml folder under res directory.
Steps for accessing the APIs:-
1. In order to make use of the device admin policies as defined in device_admin_uses_policies file in activity , we have to instantiate DevicePolicyManager class in activity as shown below:-
2. In order to perform various device admin level functionality we have to use the instance of the DevicePolicyManager.
a. This method locks the screen as if the power button is pressed.
b. This method clears all the data.
c. This method is used to set the minimum length of the password.
d. This method is used to reset the password. In the method, PASSWORD is the new password and 0 is the flag.
e. This method is used to disable the device camera.
In most of the devicePolicyManager methods, we use getActiveComponent method. This method is used to get current active component.
Here I listed the usage of some of the admin level methods.List of all the methods can be found here:
Device Admin APIs will not be enabled on its own after running the app. In order to enable them,
- Run the app
- Go to settings -> security -> Device Administrators and enable the app there.