我想用一些设备策略管理器我的应用程序中的方法。 DevicePolicyManager 是在 OS 2.2 中引入的,但我的应用程序必须继续在 OS 2.1 设备上运行。
这是我想做的事情的伪代码:
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
}
}
通过阅读文档,我认为我可能还需要设备管理接收器处理 onPasswordFailed 和 onPasswordSucceeded 回调。
来自其他 Stackoverflow 问题(例如here),我相信我有两个选择:
1. 反思
继续针对 OS 2.1 SDK 进行构建,并使用反射在运行时调用类,例如
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 });
如果我需要实现 DeviceAdminReceiver,反射会起作用吗?我将如何处理对 DeviceAdminReceiver 的回调并回调到我自己的应用程序类?
2. 有条件的类加载
更改为针对 OS 2.2 SDK 进行构建。如果当前设备版本是 OS 2.2 或更高版本,则在运行时仅加载 OS 2.2 类,例如
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();
}
这种方法看起来会产生更容易支持的代码,并且可能也可以与 DeviceAdminReceiver 一起使用。有人知道它有什么缺点或并发症吗?
所以,我的问题是:
- 您会建议使用 DevicePolicyManager 进行反射或条件类加载吗?
- 我是否需要 DeviceAdminReceiver,或者我可以检测用户是否有合适的密码,例如通过反复调用isActivePasswordSufficient在我的应用程序中确认它已经完成?
- 任何其他提示(如果您有的话)(例如这个问题表明可能存在迫使用户重置密码的问题)。
Thanks!