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