Android如何在android.telecom.Call类中进行反射(Android how do to reflection in android.telecom.Call class)

编程入门 行业动态 更新时间:2024-10-23 20:31:00
Android如何在android.telecom.Call类中进行反射(Android how do to reflection in android.telecom.Call class)

我想阻止一个电话,我尝试使用反射android.telecom.Call类与下面的代码,但这个类没有构造函数。

try { Class c = Class.forName("android.telecom.Call"); Method m = c.getMethod("disconnect"); m.setAccessible(true); Object o = m.invoke(c, new Object[] {}); } catch (Exception e) { Log.e("Exception of Reflection", e.getLocalizedMessage()); }

I want to block a phone call and i try using reflection android.telecom.Call class with following code but this class does not has constructor .

try { Class c = Class.forName("android.telecom.Call"); Method m = c.getMethod("disconnect"); m.setAccessible(true); Object o = m.invoke(c, new Object[] {}); } catch (Exception e) { Log.e("Exception of Reflection", e.getLocalizedMessage()); }

最满意答案

您需要实例化类调用,因为您将使用该实例作为方法调用的目标。 getMethod方法忽略非公共方法。 您应该使用getDeclaredMethod来查找您的私有方法。

您的代码可能如下所示:

try { Class<?> c = Class.forName("android.telecom.Call"); Object instance = c.newInstance(); Method m = c.getDeclaredMethod("disconnect"); m.setAccessible(true); m.invoke(instance, new Object[] {}); } catch (Exception e) { Log.e("Exception of Reflection", e.getLocalizedMessage()); } You need to instantiate Class Call because you'll use the instance as a target for you method invocation. The method getMethod ignores non-public method. You should use getDeclaredMethod to find your private method.

Your code may look like this:

try { Class<?> c = Class.forName("android.telecom.Call"); Object instance = c.newInstance(); Method m = c.getDeclaredMethod("disconnect"); m.setAccessible(true); m.invoke(instance, new Object[] {}); } catch (Exception e) { Log.e("Exception of Reflection", e.getLocalizedMessage()); }

更多推荐

本文发布于:2023-08-07 18:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   类中   如何在   android   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!