可以反射与属于低于最低SDK更大的使用方法? [英] Can reflection be used with methods that belong to greater than minimum sdk?

查看:176
本文介绍了可以反射与属于低于最低SDK更大的使用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有最低水平10 SDK支持的应用程序。今天,我,当我遇到一个方法来编码 setLayerType(),我需要使用。但是这种方法在 API级别11 介绍。所以,我用一种变通方法并用的反射以使用这种方法。

I am developing an app which has to support at minimum level 10 sdk. Today, I was coding when I came across a method setLayerType() which I need to use. But this method is introduced in API level 11. So, I used a workaround and used Reflection to use this method.

所以,我的问题是,会不会我的应用程序仍然崩溃的运行在API级别10?

So, my question is, won't my application still crash on device which runs on API level 10?

此问题来到我的脑海里,因为即使我使用反射在这里,不过,我打电话是在API级别11介绍的方法。将这种方法成功运行,当我在上API级别10运行还是会崩溃?Android设备上运行我的应用程序

This question came to my mind because, even though I am using Reflection here, still, I am calling a method that was introduced in API level 11. Will this method run successfully when I run my app on Android devices that run on API level 10 or will it crash?

作为源指出,该方法将独立运行OS的水平。但是,为什么要在成功API级别运行10设备它依然采用的android.jar API级别10和该文件不连含有这种方法吗?

As stated in source, the method will run independent of OS level. But, why should it run successfully on API level 10 device which is still using Android.jar of API level 10 and that file doesn't even contain this method?

我试图寻找,但没有找到一个解释。

I tried to search it but couldn't find an explanation.

PS 我想测试它与API 10级的设备上,但作为建设全面应用还需要一段时间,所以这是不可能现在来测试API级别10设备上的应用程序。

P.S. I wanted to test it on a device with API level 10, but as building full app will take some time, so it is not possible to test app on API level 10 device right now.

推荐答案

没有,当然,如果你打电话,是不是在它的API级别present的方法,应用程序将崩溃。这是真的,你是否通过反射或致电没有。

No, of course the app will crash if you call a method that is not present in its API level. This is true whether you call by reflection or not.

反思的一点是,你选择是否要基于API级调用它。比较 http://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT 的http://开发商。 android.com/reference/android/os/Build.VERSION_$c$cS.html 在code和仅与反射调用进行,如果SDK数量是够高。

The point of reflection is that you choose whether or not to call it based on the API level. Compare http://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT to http://developer.android.com/reference/android/os/Build.VERSION_CODES.html in your code and only proceed with the reflective call if the SDK number is high enough.

问题的关键是,你隔离code。与字节code引用,这不是在所有的API级别成仅由反射加载一个单独的类present任何方法或常量或类。

The point is that you isolate code with bytecode references to any method or constant or class that's not present in all API levels into a separate class that is only loaded by reflection.

下面是一些实用的code我创建只是为了管理这种事情的一个例子。它加载一个或基于API级别其他实现的接口:

Here's an example of some utility code I created just to manage this kind of thing; it loads one or the other implementation of an interface based on API level:

<一个href=\"https://$c$c.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/common/PlatformSupportManager.java?spec=svn2361&r=2361\" rel=\"nofollow\">https://$c$c.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/common/PlatformSupportManager.java?spec=svn2361&r=2361

编辑:@ FD_的回答也工作哪些惊喜我。反思是安全的,但更code。而且好像反射的做法其实是在最近不必要的,如果不是所有的Andr​​oid的JVM。

@FD_'s answer works too which surprises me. Reflection is safer, but much more code. And it seems like the reflection approach is in fact unnecessary on recent, if not all, Android JVMs.

这篇关于可以反射与属于低于最低SDK更大的使用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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