Avoid SecurityException because of "No active admin owned by"
admin, android, securityexception
Solution
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:
- 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
- Add admin receiver with xml policy file reference to Manifest.
- Install your app
- Enable app as admin in Setting>Security>Device administrators>"Your app admin receiver label value"
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);
Problem
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(); } ```