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

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

问题描述

我想用一些 DevicePolicyManager 方法在我的应用程序。该DevicePolicyManager被引入OS 2.2,但我的应用程序必须继续在OS 2.1的设备上运行。

下面是伪code代表什么,我想要做的:

 如果(needSecurity)
{
  如果(runningOS2.2orGreater)
  {
    //调用所需要的安全策略,例如
    setPasswordQuality(myComponentName,PASSWORD_QUALITY_NUMERIC)
  }
  其他
  {
    //告诉用户他们不能使用该功能
  }
}
 

从阅读的文档,我想我可能还需要一个 DeviceAdminReceiver 处理onPasswordFailed和onPasswordSucceeded回调。

从其它问题#1(例如<一个href="http://stackoverflow.com/questions/2914371/is-there-a-way-to-use-features-in-android-2-1-2-2-while-keeping-a-minsdk-version/2914757#2914757">here),我相信,我有两个选择:

1。思考

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

  MyClass类=
  ClassLoader.getSystemClassLoader()的loadClass(android.app.admin.DevicePolicyManager)

对象DPMInstance = myClass.newInstance();

方法myMethod的= myClass.getMethod(setPasswordQuality
                                    新等级[] {ComponentName.class,
                                                  int.class});
myMethod.invoke(DPMInstance,
                新对象[] {m​​yComponentName,
                               PASSWORD_QUALITY_NUMERIC});
 

如果我需要实现一个DeviceAdminReceiver,会反思工作?我将如何处理回调到DeviceAdminReceiver,回拨到我自己的应用程序类?

2。有条件的类加载

更改构建针对OS 2.2 SDK。在运行时只加载OS 2.2类,如果当前设备的版本为OS 2.2或更高版本,例如:

  INT SDK =新的整数(Build.VERSION.SDK).intValue();

如果(SDK大于7)
{
  sLog.info(OS 2.2或更高版本);
  返回新myClassImplementsDeviceAdminInterfaces();
}
其他
{
  sLog.info(OS 2.1或更早版本);
  返回新myClassDoesNotSupportDeviceAdmin();
}
 

这个方法看起来像它会更容易产生code,支持,和presumably将与DeviceAdminReceiver工作了。是任何人知道的任何缺陷或并发症呢?

所以,我的问题是:

  • 您会推荐反射或有条件的类加载使用DevicePolicyManager?
  • 请问我需要一个DeviceAdminReceiver,或者我可以检测用户是否具有合适的密码,如:通过反复调用<一href="http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#isActivePasswordSufficient%28%29"相对=nofollow>我的应用程序isActivePasswordSufficient ,以确认它已经完成?
  • 在任何其他提示如果你有他们(如<一个href="http://stackoverflow.com/questions/5475653/how-to-enforce-user-to-change-passwd-after-password-expiration-timeout-in-android">this问题表明有可能是迫使用户重新设置密码)的问题。

谢谢!

解决方案
  

如果我需要实现一个DeviceAdminReceiver,会反思工作?

不是真的。你需要使用条件类加载,这意味着你可能也只是走这条路线的开始。

  

是任何人知道的任何缺陷或并发症呢?

在谈到代表人的我的小角落里,我不知道有任何缺点。我会使用 VERSION_ codeS 常量在构建,虽然,而不是整数( 7 )。而且,除非你是支持1.5,你可以使用 SDK_INT 构建,而不是 SDK

  

你会推荐反射或有条件的类加载使用DevicePolicyManager?

有条件的类加载。

  

请问我需要一个DeviceAdminReceiver,或者我可以检测用户是否具有合适的密码,如:通过反复调用isActivePasswordSufficient在我的应用程序,以确认它已经完成?

这是我不能回答。如果你没有得到另外一个答案解决了这一点,你可能会考虑要求在自己的SO问题。

  

任何其他提示如果你有他们

从未涉足土地战争在亚洲。

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.

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

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