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()); }更多推荐
发布评论