Avoid SecurityException because of "No active admin owned by"
Asked Answered
C

6

16

How to avoid this Exception

E/AndroidRuntime(26113): Caused by: java.lang.SecurityException: No active admin owned by uid XXXX for policy #3

when calling this:

public static void lockScreen(Context context) {
    Log.d(TAG, "lockScreen");
    ComponentName mDeviceAdminSample = null;
    DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
    dpm.lockNow();
}
Cariecaries answered 5/6, 2012 at 20:57 Comment(0)
F
9

I got the same error as OP. Since only combination of other answers helped me, here is my walk through to make the OP code sample work:

  1. Make sure you have created device admin receiver and xml policy file as described in https://developer.android.com/guide/topics/admin/device-admin.html
  2. Add admin receiver with xml policy file reference to Manifest.
  3. Install your app
  4. Enable app as admin in Setting>Security>Device administrators>"Your app admin receiver label value"
  5. or do [4] programmatically

    mDeviceAdminSample = new ComponentName(this,DeviceAdminSampleReceiver.class);
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
    currActivity.startActivityForResult(intent, 0);
    
Forced answered 13/4, 2018 at 15:46 Comment(0)
G
5

You have to install and enable a device administrator as described in the reference docs: http://developer.android.com/guide/topics/admin/device-admin.html

Gazpacho answered 6/6, 2012 at 2:40 Comment(0)
I
3

you are missing this below line.

mDeviceAdminSample = new ComponentName(this, DeviceAdminSampleReceiver.class);
Imprisonment answered 29/5, 2015 at 12:0 Comment(0)
C
2

Notice how the lines:

        Intent it = new         Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        it.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, YourAdminReceiver.class));
        startActivityForResult(it, 0);

can be skipped. If you do not want your users to allow the app to lock the screen there is the alternative way:

adb shell dpm set-device-owner com.package/.YourAdminReceiver

Now what does this have to do with the SecurityException the OP was encountering ?

Well, I already had the device owner set to com.package/.YourAdminReceiver via the adb shell way, but for different purposes than screen locking. When I have added the <force-lock> tag to my device-admin.xml:

<uses-policies>
        <limit-password />
        <reset-password />
        <force-lock />
    </uses-policies>

I was naturally expecting my locknow() API call to work, but instead it hasn't. I got the SecurityException just as OP did. Strange, given that my app was already the admin and owner of the device. I have made it work by unsetting from the device owner role, uninstalling it and then reinstalling it. Be advised that once set as a device owner via adb shell, an app can only be unset from that role from code:

//        mDpm.clearDeviceOwnerApp(getPackageName());
//        mDpm.removeActiveAdmin(deviceAdmin);
Cassycast answered 10/1, 2017 at 14:11 Comment(0)
N
0

In my case, the device admin was part of an instrumentation tests rather than my app itself. Turned out to be a bug in the way Android handles permissions. The fix was to move the device admin class, manifest and policies into the target app. Certainly not ideal, but I can't see a way around it.

Nocturne answered 27/6, 2020 at 12:34 Comment(0)
N
0

You would need 3 thing to do :

  1. add <receiver> to AndroidManifest
  2. make new xml file for <device-admin>
  3. make new .kt file for DeviceAdminReceiver

Here is a link from an answer I already did (with example code) : stackoverflow.com

A reference from developer.android.com

Nafis answered 3/6, 2021 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.