Disable Android Application Uninstallation using Device Administration

Android platform offers system-level device management capabilities through the Device Administration APIs. If an application package component is registered as system administrator, it can provide device administration features at the system level. An active system admin package can not be uninstalled until removed as system administrator. We will make benefits of this property to disable android application uninstallation by registering and activating system admin component.

 Sample Application


[su_button url=”https://github.com/androidsrc/DeviceAdminSample” target=”blank” style=”stroked” background=”#51d461″ color=”#ffffff” size=”6″ center=”yes” radius=”0″ icon=”icon: arrow-circle-o-down”]Download Full Source Code[/su_button]


[su_carousel source=”media: 244,245,247,246,248,249″ link=”lightbox” width=”700″ height=”400″ title=”no” mousewheel=”no”]


In AndroidManifest.xml,
1. We need to declare a receiver which will subclass DeviceAdminReceiver.
2. Permission attribute should be declared with value android.permission.BIND_DEVICE_ADMIN
3. meta-data tag defining device admin policies usage.
4. intent-filter for admin enable, admin disable and admin disable request.

            android:permission="android.permission.BIND_DEVICE_ADMIN" >
                android:resource="@xml/device_admin" />

                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />

Create file /res/xml/device_admin.xml, this file will define system label capabilities granted on activation. I have not defined any policies as out motto is to just register and activate a system admin to make our application not uninstallable. Just to brief I have mentioned type of policy management you can be granted.

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >


		<limit-password />

        <watch-login />

        <reset-password />

        <force-lock />

        <wipe-data />

        <expire-password />

        <encrypted-storage />

        <disable-camera />


Create receiver class SampleDeviceAdminReceiver.java and it will extend DeviceAdminReceiver. We will override few functions to get admin enable and disable status.

package com.androidsrc.sampledeviceadmin;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SampleDeviceAdminReceiver extends DeviceAdminReceiver {

	public void onDisabled(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "disabled dpm", Toast.LENGTH_SHORT).show();
		super.onDisabled(context, intent);

	public void onEnabled(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "enabled dpm", Toast.LENGTH_SHORT).show();
		super.onEnabled(context, intent);

	public CharSequence onDisableRequested(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "disable dpm request", Toast.LENGTH_SHORT).show();
		return super.onDisableRequested(context, intent);

Create a class PolicyManager.java, we will define api to check admin active status and removing admin component.

package com.androidsrc.sampledeviceadmin;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;

public class PolicyManager {

    public static final int DPM_ACTIVATION_REQUEST_CODE = 100;

    private Context mContext;
    private DevicePolicyManager mDPM;
    private ComponentName adminComponent;

    public PolicyManager(Context context) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        mDPM = (DevicePolicyManager) mContext
        adminComponent = new ComponentName(mContext.getPackageName(),
                mContext.getPackageName() + ".SampleDeviceAdminReceiver");

    public boolean isAdminActive() {
        return mDPM.isAdminActive(adminComponent);

    public ComponentName getAdminComponent() {
        return adminComponent;

    public void disableAdmin() {

In our main layout /res/layout/activity_main.xml, we will add two buttons to activate and deactivate admin.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.androidsrc.sampledeviceadmin.MainActivity" >

        android:text="Activate Admin"
        android:textStyle="bold" />

        android:text="Deactivate Admin"
        android:textStyle="bold" />

In application’s launcher activity MainActivity.java in sample app, we will implement View.OnCLickListener to handle onclick events of buttons. For activation of admin, we need to start activity with intent action as DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN. And to specify admin component we will add extars with Key as DevicePolicyManager.EXTRA_DEVICE_ADMIN. You can attach admin activation explanation as well so that user know your intention to do it.

Once you enable admin component from your package, your application can not be uinstalled until it is deactivated. It can be verified by uninstalling it or in application info. No other application can remove your active component from there source, it can be removed only if call is from package which is owner of this admin component.

Once you disable this admin, your application can be uninsatlled.

package com.androidsrc.sampledeviceadmin;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

    private PolicyManager policyManager;

    protected void onCreate(Bundle savedInstanceState) {

        policyManager = new PolicyManager(this);

    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.activate_admin:
                if (!policyManager.isAdminActive()) {
                    Intent activateDeviceAdmin = new Intent(
                                    "After activating admin, you will be able to block application uninstallation.");
            case R.id.deactivate_admin:
                if (policyManager.isAdminActive())

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode == Activity.RESULT_OK
                && requestCode == PolicyManager.DPM_ACTIVATION_REQUEST_CODE) {
            // handle code for successfull enable of admin
        } else {
            super.onActivityResult(requestCode, resultCode, data);

Many applications featuring enterprise management or application lock apps use this feature. Option is provided to user to enable this feature. Though a sophisticated android user can always activate/deactiavte your active system admin from Settings->Security->Device Administrators.

You may also like...

2 Responses

  1. Menashe Ungar says:

    I am working on a sort of simplified MDM for my company, but the fact that a user can disable an administrator so simply makes all this stuff unusable. Is there a way for a device company owned to prevent this?

  2. mgodot says:

    555555,not work in android 6.0,

Leave a Reply

Your email address will not be published. Required fields are marked *