7

What I Want

I want to have a simple checkbox in my settings menu, which if checked will ENABLE Device Administration for my app and will prevent my app from getting uninstalled.

The checkbox when unchecked will DISABLE Device Administration.

My app is about security and needs to be protected from getting uninstalled. Can I get a simple solution for this?

PS - I have read the documentation about this, but can't seem to get it working.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • Possible duplicate of [How to prevent an application from being uninstalled?](https://stackoverflow.com/q/7540002/608639). – jww Sep 18 '18 at 08:10

2 Answers2

13

This is not possible. You cannot decide on your own to make your app be a device administrator. You are welcome to lead the user over to the appropriate spot in the Settings app to have the user elect to make your app be a device administrator, though, via ACTION_ADD_DEVICE_ADMIN.

For example, this activity will see if it is already a device admin (via isActiveAdmin()), then will launch an ACTION_ADD_DEVICE_ADMIN activity if needed:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.lockme;

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

public class LockMeNowActivity extends Activity {
  private DevicePolicyManager mgr=null;
  private ComponentName cn=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    cn=new ComponentName(this, AdminReceiver.class);
    mgr=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
  }

  public void lockMeNow(View v) {
    if (mgr.isAdminActive(cn)) {
      mgr.lockNow();
    }
    else {
      Intent intent=
          new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
      intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
      intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                      getString(R.string.device_admin_explanation));
      startActivity(intent);
    }
  }
}

(from this sample project)

The two extras (EXTRA_DEVICE_ADMIN and EXTRA_ADD_EXPLANATION) are optional, though they are a good idea. The first one should be a ComponentName identifying your DeviceAdminReceiver subclass; the second one should be a string (from a string resource) that explains why the user should make your app be a device admin.

My app is about security and needs to be protected from getting uninstalled.

Since anybody can go in and decide to not make your app be a device administrator (again, through Settings), then uninstall it, this is not much of a defense.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This is what I am actually referring to. I want to direct the user to elect if he/she wants to make the app device administrator or not. – Aritra Roy Mar 01 '15 at 15:07
  • But unfortunately, I can't seem to get it working. Can you help with with the steps for it? – Aritra Roy Mar 01 '15 at 15:08
  • @Aritra: Well, it's just an `Intent` action, so there's not much to it. I have pasted a sample into my answer. – CommonsWare Mar 01 '15 at 15:14
  • I seem to be making some very minor mistake. I will check your code and try it. Thanks a ton. :-) – Aritra Roy Mar 01 '15 at 15:15
  • @CommnsWare Should I not declare a receiver for this in the manifest? – Aritra Roy Mar 01 '15 at 15:18
  • @Aritra: You have to have a `DeviceAdminReceiver` for device administration to work. You don't have to really *use* that receiver -- if you look at the sample project, it's subclass of `DeviceAdminReceiver` is basically empty. But you do need it to exist and be registered appropriately in the manifest. – CommonsWare Mar 01 '15 at 15:19
  • Perfect! I will be doing it that way. – Aritra Roy Mar 01 '15 at 15:20
  • Thank you bro. Saved my life. And to those of you whose code doesn't seem to be working, please check the `android manifest` file. We need to add `` and `` codes – viper Oct 16 '17 at 11:47
9
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * This is the component that is responsible for actual device administration.
 * It becomes the receiver when a policy is applied. It is important that we
 * subclass DeviceAdminReceiver class here and to implement its only required
 * method onEnabled().
 */
public class DemoDeviceAdmin extends DeviceAdminReceiver {
    static final String TAG = "DemoDeviceAdmin";

    /** Called when this application is approved to be a device administrator. */
    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Toast.makeText(context, R.string.device_admin_enabled,
                Toast.LENGTH_LONG).show();
        Log.d(TAG, "onEnabled");
    }

    /** Called when this application is no longer the device administrator. */
    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Toast.makeText(context, R.string.device_admin_disabled,
                Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.d(TAG, "onPasswordChanged");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
        Log.d(TAG, "onPasswordFailed");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
        Log.d(TAG, "onPasswordSucceeded");
    }

}

And the MainActivity goes like this

devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        demoDeviceAdmin = new ComponentName(this, DemoDeviceAdmin.class);
        Log.e("DeviceAdminActive==", "" + demoDeviceAdmin);

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);// adds new device administrator
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, demoDeviceAdmin);//ComponentName of the administrator component.
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "Disable app");//dditional explanation
        startActivityForResult(intent, ACTIVATION_REQUEST);

And Manifest goes like this :

 <!-- This is where we register our receiver -->
        <receiver
            android:name=".DemoDeviceAdmin"
            android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <intent-filter>

                <!-- This action is required -->
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

            <!-- This is required this receiver to become device admin component. -->
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin_sample" />
        </receiver>