在 Android 上使用 OS 2.2 DevicePolicyManager SDK 类,同时支持 OS 2.1 设备 [英] Using OS 2.2 DevicePolicyManager SDK classes on Android whilst supporting OS 2.1 devices

查看:13
本文介绍了在 Android 上使用 OS 2.2 DevicePolicyManager SDK 类,同时支持 OS 2.1 设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序.DevicePolicyManager 是在 OS 2.2 中引入的,但我的应用程序必须继续在 OS 2.1 设备上运行.

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.

这是我想做的伪代码:

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
  }
}

通过阅读文档,我想我可能还需要一个 DeviceAdminReceiver 处理 onPasswordFailed 和 onPasswordSucceeded 回调.

From reading the docs I think I may also need a DeviceAdminReceiver to handle onPasswordFailed and onPasswordSucceeded callbacks.

来自其他 Stackoverflow 问题(例如 这里),我相信我有两个选择:

From other Stackoverflow questions (e.g. here), I believe I have two options:

1.反射

继续针对 OS 2.1 SDK 构建并使用反射在运行时调用类,例如

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 });                            

如果我需要实现 DeviceAdminReceiver,反射会起作用吗?我将如何处理对 DeviceAdminReceiver 的回调并回调到我自己的应用程序类中?

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.条件类加载

更改为针对 OS 2.2 SDK 构建.如果当前设备版本是 OS 2.2 或更高版本,则在运行时仅加载 OS 2.2 类,例如

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();
}

这种方法看起来会生成更容易支持的代码,并且大概也可以与 DeviceAdminReceiver 一起使用.有人知道它有什么缺点或并发症吗?

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?

所以,我的问题是:

  • 您会建议使用 DevicePolicyManager 进行反射或条件类加载吗?
  • 我是否需要 DeviceAdminReceiver,或者我可以检测用户是否有合适的密码,例如通过在我的应用程序确认它已经完成?
  • 如果您有任何其他提示(例如 this question 表明可能存在强制用户重置密码的问题).
  • 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).

谢谢!

推荐答案

如果我需要实现 DeviceAdminReceiver,反射会起作用吗?

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?

代表我的任何人"小角落发言,我不知道任何缺点.不过,我会在 Build 上使用 VERSION_CODES 常量,而不是整数 (7).而且,除非您支持 1.5,否则您可以在 Build 上使用 SDK_INT 而不是 SDK.

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.

您会建议使用 DevicePolicyManager 进行反射或条件类加载吗?

Would you recommend reflection or conditional class loading for using DevicePolicyManager?

条件类加载.

我是否需要 DeviceAdminReceiver,或者我可以检测用户是否有合适的密码,例如通过在我的应用中反复调用 isActivePasswordSufficient 来确认它已经完成?

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?

我无法回答.如果您没有得到解决这一点的另一个答案,您可以考虑在其自己的 SO 问题中提出这个问题.

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.

如果您有任何其他提示

永远不要卷入亚洲的陆战.

Never get involved in a land war in Asia.

这篇关于在 Android 上使用 OS 2.2 DevicePolicyManager SDK 类,同时支持 OS 2.1 设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆