Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid SecurityException because of "No active admin owned by"

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();
}
like image 601
ViliusK Avatar asked Jun 05 '12 20:06

ViliusK


1 Answers

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);
    
like image 153
VadimK Avatar answered Sep 20 '22 11:09

VadimK