Avoid SecurityException because of "No active admin owned by"

ViliusK picture ViliusK · Jun 5, 2012 · Viewed 16.3k times · Source

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();
}

Answer

VadimK picture VadimK · Apr 13, 2018

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);