Android 安卓和反射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1754714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:35:39  来源:igfitidea点击:

Android and reflection

android

提问by Wilfred Springer

I get the impression that Android supports reflection. But does it really? How sensible would it be to rely on reflection heavily? What's the penalty?

我的印象是 Android 支持反射。但真的吗?严重依赖反思有多明智?有什么惩罚?

采纳答案by Will

Android supports reflection.

Android 支持反射。

Once you've got a prototype running, you can benchmark and determine your bottlenecks.

原型运行后,您可以进行基准测试并确定瓶颈。

If its reflection, then consider trying to cache interfaces and such to make it a one-off cost, rather than continually resolving the same interfaces from the same instances repeatedly.

如果是反射,则考虑尝试缓存接口等以使其成为一次性成本,而不是不断重复地从相同的实例解析相同的接口。

回答by Chris Boyle

It is supported, and even recommended in the situation where you want compatibility with multiple versions of the Android OS in one apkfile. This article from the official Android Developers Blog describes how to build an app that requires only some early version of the API, and uses reflection to invoke new APIs if they are available:

它受支持,甚至在您希望在一个apk文件中与多个版本的 Android 操作系统兼容的情况下也是推荐的。这篇来自官方 Android 开发者博客的文章描述了如何构建一个只需要一些早期版本 API 的应用程序,并使用反射来调用可用的新 API:

Backward compatibility for Android applications

Android 应用程序的向后兼容性

回答by Mustafa Güven

a simple example related to using reflection on android http://aaarkonusurum.blogspot.com/2012/02/android-ile-reflection.html

一个与在 android 上使用反射相关的简单示例 http://aaarkonusurum.blogspot.com/2012/02/android-ile-reflection.html

Class x = Object.class;
Constructor[] constructors = x.getDeclaredConstructors();
Field[] fields = x.getDeclaredFields();
Method[] methods = x.getDeclaredMethods();
for (Constructor constructor : constructors) { 
    //constructors
}
for (Field field : fields) {
    //fields
}
for (Method method : methods) {
    //methods
}    



Creating a TextView from codebehind at runtime with using reflection



使用反射在运行时从代码隐藏创建 TextView

String x = TextView.class.toString().replace("class ", "");
Class<?> cls = Class.forName(x);
Class<?> param[] = new Class[1];
param[0] = Context.class; //Context=o an ki context ==> [activity.class]
Constructor<?> ct = cls.getConstructor(param);
Object paramVal[] = new Object[1];
paramVal[0] = context;
Object retobj = ct.newInstance(paramVal); 



Reaching to setText() method at the runtime



在运行时到达 setText() 方法

Class methodParam[] = new Class[1];
methodParam[0] = java.lang.CharSequence.class;
Method method = cls.getMethod("setText", methodParam);
Object arglist[] = new Object[1];
arglist[0] = new String("THIS TEXTVIEW HAS BEEN CREATED ON RUN TIME");
method.invoke(retobj, arglist); 

回答by pgsandstrom

There is a nice example of reflection in the sample code as well, in BusinessCard. This method wont result in a bunch of expections being thrown, so it should be much more performance friendly. It is also, in my opinion, easier to implement. Especially if it concerns a previously unimplemented method.

在示例代码中也有一个很好的反射示例,在 BusinessCard 中。这种方法不会导致抛出一堆期望,因此它应该对性能更加友好。在我看来,它也更容易实现。特别是如果它涉及以前未实现的方法。

Here is where it is used: http://developer.android.com/resources/samples/BusinessCard/src/com/example/android/businesscard/ContactAccessor.html

这是使用它的地方:http: //developer.android.com/resources/samples/BusinessCard/src/com/example/android/businesscard/ContactAccessor.html

回答by user542954

Android of course supports Reflection and we can read methods of a different APK or Framework class. Here is an article on using Reflection in Android as a possible design approach to create API- http://prasanta-paul.blogspot.kr/2013/09/java-reflection-as-android-api.html

Android 当然支持反射,我们可以读取不同 APK 或框架类的方法。这是一篇关于在 Android 中使用反射作为创建 API 的可能设计方法的文章- http://prasanta-paul.blogspot.kr/2013/09/java-reflection-as-android-api.html