I want to use some DevicePolicyManager methods in my application. DevicePolicyManager was introduced in OS 2.2, but my application should continue to run on OS 2.1 devices.
Here's the pseudo code for what I want to do:
if (needSecurity) { if (runningOS2.2orGreater) {
From reading the docs, I think I might also need DeviceAdminReceiver to handle the onPasswordFailed and onPasswordSucceeded callbacks.
Of the other questions related to Stackoverflow (e.g. here ), I believe that I have two options:
1. Reflection
Continue building against the OS 2.1 SDK and use reflection to call classes at runtime, for example.
Class myClass = ClassLoader.getSystemClassLoader().loadClass("android.app.admin.DevicePolicyManager") Object DPMInstance = myClass.newInstance(); Method myMethod = myClass.getMethod("setPasswordQuality", new Class[] { ComponentName.class, int.class }); myMethod.invoke(DPMInstance, new Object[] { myComponentName, PASSWORD_QUALITY_NUMERIC });
If I need to implement DeviceAdminReceiver, will the reflection work? How do I handle callbacks in DeviceAdminReceiver and migrate to my own application classes?
2. Loading a conditional class
Edit to create against OS 2.2 SDK. At run time, only OS 2.2 classes are loaded if the current device version is OS 2.2 or later, for example.
int sdk = new Integer(Build.VERSION.SDK).intValue(); if (sdk > 7) { sLog.info("OS 2.2 or later"); return new myClassImplementsDeviceAdminInterfaces(); } else { sLog.info("OS 2.1 or earlier"); return new myClassDoesNotSupportDeviceAdmin(); }
This approach looks like it will create simpler code for support and, apparently, will work with DeviceAdminReceiver as well. Does anyone know of any flaws or complications?
So my questions are:
- Would you recommend reflection or conditional class loading for using DevicePolicyManager?
- I need a DeviceAdminReceiver, or I can determine if the user has a suitable password, for example. repeating isActivePasswordSufficient in your application to confirm that it was done?
- Any other tips, if you have one (for example, this question , suggest that there may be problems causing the user to reset their password).
Thanks!