简体   繁体   English

反射对Android有用吗?

[英]Is reflection useful for Android?

I learnt a bit about reflection after reading about it in some tpics here. 在阅读了一些有关tpic的内容后,我对反射有所了解。 From what I understands, it is used to check the avaibility of a certain class/method/field at runtime. 据我了解,它用于在运行时检查某个类/方法/字段的可用性。 But is it really useful in Android ? 但这在Android中真的有用吗? Android provide us with the api version at runtime and we can know if a particular class/method or field is available by reading the Android doc (or with error message with Android Studio). Android在运行时为我们提供了api版本,通过阅读Android文档(或带有Android Studio的错误消息),我们可以知道特定的类/方法或字段是否可用。

I understand how it can be useful with Java in general, but is there any meaning to use it in Android? 我了解它通常在Java中如何有用,但是在Android中使用它有任何意义吗?

Reflection (in every languages) is very powerful. 反思(每种语言)都非常强大。

In Android most of time reflection is not needed, because you can find Security Exceptions, problems. 在Android中,大多数时间都不需要反思,因为您会发现安全异常,问题。 It depends on what You do. 这取决于您的工作。

If you use undocumented classes, libs, you can use it, and it's very useful. 如果使用未记录的类,库,则可以使用它,这非常有用。

Sometimes, to do particular things, like turn on/off 3g on old device, change device language, you need rooted device to use reflection. 有时,要执行特定的操作,例如在旧设备上打开/关闭3g,更改设备语言,则需要扎根的设备才能使用反射。

Finally, depends always on what You do. 最后,始终取决于您的工作。

Sometimes it works , and some times it does't work . 有时可行,有时无效。

ET work example : You can reflect the method to hang off a phone call (there are a lot example codes on Internet so I won't copy the code.). ET工作示例:您可以反映出挂断电话的方法(Internet上有很多示例代码,因此我不会复制该代码。)。

Doesn't work example: If you want to switch data connect status , use reflection works on 4.4 but will not work on 5.0 because it's a binder connection, the BN will check Permission the app granted , but this permission only granted to system app . 无效示例:如果要切换数据连接状态,则使用反射在4.4上有效,但在5.0上则无效,因为它是活页夹连接,BN会检查已授予该应用的权限,但此权限仅授予系统app。 So if your app is a third part app,on 5.0 you can't use reflection to switch data connect status. 因此,如果您的应用程序是第三方应用程序,则在5.0上,您将无法使用反射来切换数据连接状态。

Hope that helps 希望能有所帮助

This is a very general question, it really depends on what you're trying to do. 这是一个非常笼统的问题,它实际上取决于您要执行的操作。 Sometimes you have to use reflection, if the APIs are hidden, all depends on your use case, generally you should avoid reflection as it complicates your code more than its needs to be and its potentially unsafe for further versions of android. 有时,您必须使用反射,如果API被隐藏,则全都取决于您的用例,通常应避免使用反射,因为反射会使您的代码变得比所需的复杂得多,并且对于进一步的android版本而言可能不安全。

In my opinion it's a good to way to do particular things. 我认为这是做特定事情的好方法。 For example you can use the methods of PowerProfile class to do a simple power model for your phone. 例如,您可以使用PowerProfile类的方法为手机创建简单的电源模型。

By the method getAveragePower(POWER_WIFI_SCAN) you can take the average current in mA consumed by the subsystem (in this case: wi-fi during scan). 通过getAveragePower(POWER_WIFI_SCAN)方法,您可以获取子系统消耗的平均电流(单位为mA)(在这种情况下:扫描过程中为wi-fi)。

So to use PowerProfile's methods for get your battery capacity you you could use java reflection in this way: 因此,要使用PowerProfile的方法来获取电池电量,您可以通过以下方式使用Java反射:

private Object mPowerProfile_;
private static final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
private Double batteryCapacity = Double.valueOf(1);

public Double getBatteryCapacity(Context ctx) {

    try {
mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS).getConstructor(Context.class).newInstance(this);
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

try {
batteryCapacity = (Double) Class.forName(POWER_PROFILE_CLASS).getMethod("getAveragePower", String.class).invoke(mPowerProfile_, "battery.capacity");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM