Run intent DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN from a service
The reason is on the code of the Android DeviceAdminAdd class itself:
if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task");
finish();
return;
}
You should consider using another activity to call the DevicePolicyManager.
I've just fixed such issue for myself.
Note, that you need to put this code inside parent in Android Manifest.xml file:
<receiver
android:name=".ScreenLockerDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_policies" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
and it works :)