What is a use of device administrator in android?

Amol Wadekar picture Amol Wadekar · Sep 24, 2013 · Viewed 10.3k times · Source

I have doubt/question about device administrator api, using this API can I limit my application from uninstillation by any way, may be by using password.

Please tell me what exactly device administrator api does? How I can used them in my application? And how I can decide which Policies I need to set?

Please help me in this confusion.

Answer

Nikhil picture Nikhil · Mar 8, 2017

Device Administrator API is an API that provides device administration features at the system level. These APIs allow you to create security-aware applications. It is used to make your application uninstall from the device or to capture a picture by using a camera when the screen is a lock.

The Device Administration API supports the policies like,
1.) Password enabled - Requires that devices ask for PIN or passwords.

2.) Minimum password length - Set the required number of characters for the password. For example, you can require PIN or passwords to have at least six characters.

3.) Alphanumeric password required - Requires that passwords have a combination of letters and numbers. They may include symbolic characters.

4.) Complex password required - Requires that passwords must contain at least a letter, a numerical digit, and a special symbol. Introduced in Android 3.0.

5.) Minimum letters required in password - The minimum number of letters required in the password for all admins or a particular one. Introduced in Android 3.0.

6.) Minimum lowercase letters required in password - The minimum number of lowercase letters required in the password for all admins or a particular one. Introduced in Android 3.0.

7.) Password expiration timeout - When the password will expire, expressed as a delta in milliseconds from when a device admin sets the expiration timeout. Introduced in Android 3.0.

8.) Maximum failed password attempts - Specifies how many times a user can enter the wrong password before the device wipes its data. The Device Administration API also allows administrators to remotely reset the device to factory defaults. This secures data in case the device is lost or stolen.

9.) Maximum inactivity time lock - Sets the length of time since the user last touched the screen or pressed a button before the device locks the screen. When this happens, users need to enter their PIN or passwords again before they can use their devices and access data. The value can be between 1 and 60 minutes.

10.) Disable camera - Specifies that the camera should be disabled. Note that this doesn't have to be a permanent disabling. The camera can be enabled/disabled dynamically based on context, time, and so on. Introduced in Android 4.0.

To use this Device Administrator in your application, you should use the below code. You need to just add this class to your project.

public class MyDeviceAdminReceiver extends DeviceAdminReceiver {

private static final int CAMERA_REQUEST = 1888;
private SurfaceView sv;
private boolean safeToTakePicture = false;
private int count = 0;

/**
 * method to show toast
 *
 * @param context the application context on which the toast has to be displayed
 * @param msg     The message which will be displayed in the toast
 */
private void showToast(Context context, CharSequence msg) {
    Log.e("MyDeviceAdminRec...", "::>>>>1 ");
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onEnabled(Context context, Intent intent) {
    Log.e("MyDeviceAdminRec...", "::>>>>2 ");
    showToast(context, "Sample Device Admin: enabled");
}

@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
    Log.e("MyDeviceAdminRec...", "::>>>>3 ");
    return "This is an optional message to warn the user about disabling.";
}

@Override
public void onDisabled(Context context, Intent intent) {
    Log.e("MyDeviceAdminRec...", "::>>>>4 ");
    showToast(context, "Sample Device Admin: disabled");
}

@Override
public void onPasswordChanged(Context context, Intent intent) {
    Log.e("MyDeviceAdminRec...", "::>>>>5 ");
    showToast(context, "Sample Device Admin: pw changed");
}

@Override
public void onPasswordFailed(Context context, Intent intent) {
    Log.e("MyDeviceAdminRec...", "::>>>>6 ");
    showToast(context, "Sample Device Admin: pw failed");
}

@Override
public void onPasswordSucceeded(Context context, Intent intent) {
    Log.e("MyDeviceAdminRec...", "::>>>>7 ");
    showToast(context, "Sample Device Admin: pw succeeded");
}
}

Also, Create a file as named device_admin_uses_policies into
res->xml->device_admin_uses_policies and add the below into it.

<uses-policies>
    <limit-password />
    <watch-login />
    <reset-password />
    <force-lock />
    <wipe-data />
    <expire-password />
    <encrypted-storage />
    <disable-camera />
</uses-policies>

At last, add this code to the manifest file.

<receiver
    android:name=".MyDeviceAdminReceiver"
    android:description="@string/app_name"
    android:label="@string/app_name"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/device_admin_uses_policies"/>

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.ACTION_PASSWORD_CHANGED" />
            <action android:name="android.app.action.ACTION_PASSWORD_FAILED" />
            <action android:name="android.app.action.ACTION_PASSWORD_SUCCEEDED" />
        </intent-filter>
    </receiver>

It will work as a background service & notify you. It works for me in a great way. I hope it will also work for you.