Using OS 2.2 DevicePolicyManager SDK classes on Android whilst supporting OS 2.1 devices
I want to use some DevicePolicyManager methods in my app. The DevicePolicyManager was introduced in OS 2.2, but my app must continue to run on OS 2.1 devices.
Here's pseudocode for what I want to do:
if (needSecurity)
{
if (runningOS2.2orGreater)
{
// Invoke the required security policy, e.g.
setPasswordQuality(myComponentName, PASSWORD_QUALITY_NUMERIC)
}
else
{
// Tell the user they can't use this feature
}
}
From reading the docs I think I may also need a DeviceAdminReceiver to handle onPasswordFailed and onPasswordSucceeded callbacks.
From other Stackoverflow questions (e.g. here), I believe I have two options:
1. Reflection
Continue to build against OS 2.1 SDK and use reflection to invoke classes at runtime, e.g.
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 a DeviceAdminReceiver, will reflection work? How would I handle callbacks to DeviceAdminReceiver and call back into my own application classes?
2. Conditional class loading
Change to build against the OS 2.2 SDK. At runtime only load the OS 2.2 classes if the current device version is OS 2.2 or newer, e.g.
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 produce easier code to support, and presumably will work with a DeviceAdminReceiver too. Is anybody aware of any drawbacks or complications to it?
So, my questions are:
- Would you recommend reflection or conditional class loading for using DevicePolicyManager?
- Will I need a DeviceAdminReceiver, or can I detect whether the user has a suitable password, e.g. by repeatedly calling isActivePasswordSufficient in my app to confirm it has been done?
- Any other tips if you have them (e.g. this question suggests there might be issues forcing the user to reset their password).
Thanks!
If I need to implement a DeviceAdminReceiver, will reflection work?
Not really. You'd need to use conditional class loading, which means you may as well just go that route to begin with.
Is anybody aware of any drawbacks or complications to it?
Speaking on behalf of my little corner of "anybody", I'm not aware of any drawbacks. I'd use the VERSION_CODES
constants on Build
, though, rather than the integer (7
). And, unless you're supporting 1.5, you can use SDK_INT
on Build
rather than SDK
.
Would you recommend reflection or conditional class loading for using DevicePolicyManager?
Conditional class loading.
Will I need a DeviceAdminReceiver, or can I detect whether the user has a suitable password, e.g. by repeatedly calling isActivePasswordSufficient in my app to confirm it has been done?
That I can't answer. If you don't get another answer addressing this point, you might consider asking that in its own SO question.
Any other tips if you have them
Never get involved in a land war in Asia.
精彩评论