4

I am unable to get the Activity which allows the user to grant permission for an app to be a device admin to work.

My code is as follows...

ComponentName comp = new ComponentName(this, CustomReceiver.class);

Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

i.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, comp);
i.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Explanation");

startActivity(i);

The application does not crash / report an exception. What could I be doing wrong ?

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
  • 2
    A little late but...You need to use startActivityForResult() as nitinreddy suggested but you cannot do that from a service. To do so from a service, the best thing to do would be start a transparent activity, request admin rights from that activity, and then finish() that activity. – Jasjit Singh Marwah Dec 21 '12 at 19:14
  • http://stackoverflow.com/a/17297711/581159 this may help you... – Zeba Oct 22 '13 at 06:02

2 Answers2

1

Something like this would do

if (!mPolicy.isAdminActive()) {

    Intent activateDeviceAdminIntent =
        new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

    activateDeviceAdminIntent.putExtra(
        DevicePolicyManager.EXTRA_DEVICE_ADMIN,
        mPolicy.getPolicyAdmin());

    // It is good practice to include the optional explanation text to
    // explain to user why the application is requesting to be a device
    // administrator. The system will display this message on the activation
    // screen.
    activateDeviceAdminIntent.putExtra(
        DevicePolicyManager.EXTRA_ADD_EXPLANATION,
        getResources().getString(R.string.device_admin_activation_message));

    startActivityForResult(activateDeviceAdminIntent,
        REQ_ACTIVATE_DEVICE_ADMIN);
}

May be you are not considering

mPolicy.getPolicyAdmin()
nithinreddy
  • 6,167
  • 4
  • 38
  • 44
  • Hi first of all thanks a lot for the response. What is "getPolicyAdmin" a member of ? I can't seem to find it to be a method of DevicePolicyManager ? – Heshan Perera May 14 '12 at 10:18
  • 1
    No, is CustomReceiver the Component which references deviceadmin class? – nithinreddy May 14 '12 at 10:57
  • Meaning the one which extends DeviceAdminReceiver ? would you mind sharing that bit of code with me ? Just editing the above answer would do. – Heshan Perera May 14 '12 at 11:00
  • Sure, I will just look into the sample I have, but you should be able to figure it out from here if you didn't check the link. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html – nithinreddy May 14 '12 at 11:04
1

Here is a clear example on how to do it, (the official docs here and here miss some context)

//class that implements DeviceAdminReceiver, defined in the Manifest 
ComponentName deviceAdminCN = new ComponentName(context, DeviceAdminReceiverImpl.class) 

...

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminCN);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "your explanation for the user here");
startActivityForResult(intent, YOUR_REQUEST_CODE);

Here is the reference class used in the official sample app.

Patrick
  • 33,984
  • 10
  • 106
  • 126