如何正确使用反射来访问电话管理器隐藏方法 [英] How to properly use reflection to access hidden methods in Telephony Manager
问题描述
我不知道如果我在与反思自身,或者我想获得方法麻烦。
I'm not sure if I'm having trouble with Reflection itself, or the method I'm trying to obtain.
我想要做的就是调用从类功能setLine1Number:
What I'd like to do is call the function setLine1Number from the class:
com.android.internal.telephony.gsm.GSMPhone
所以,我可以有,因为它是不是在我的SIM卡需要我的电话号码正确插入我的手机。因此,我希望能够调用函数getLine1Number,并使其返回,我设置了相同的编号。
So that I can have my number properly inserted into my phone as it is not required to be in my SIM. Therefore I want to be able to call the function getLine1Number and have it return the same number that I set.
反射出现,以便能够使用该功能,因为它是不公开的API中的唯一途径。
Reflection appears the only way to be able to use this function as it is not in the public API.
我已经写了这一点,但不断收到非法参数异常。这里是我的code:
I've written this, but keep getting an illegal argument exception. Here is my code:
String className = "com.android.internal.telephony.gsm.GSMPhone";
Class classToInvestigate = Class.forName(className);
Object arglist[] = new Object[3];
arglist[0] = new String("Phone Number");
arglist[1] = new String ("16035552412"); // Not a real phone number
arglist[2] = null;
Class[] paramTypes = new Class[3];
paramTypes[0] = String.class;
paramTypes[1] = String.class;
paramTypes[2] = Message.class;
Method setLine1Number = classToInvestigate.getMethod("setLine1Number", paramTypes);
setLine1Number.setAccessible(true);
Object TestReturn = setLine1Number.invoke(classToInvestigate, arglist); // Problem is here. Not sure how to properly do this.
现在在这一点上,我想,如果我可以叫
Now at this point, I would like if I could call
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String PhoneNumber2 = telephonyManager.getLine1Number();
和使其返回,我已经输入数字。但正如我所说的非法参数异常,我不知道到底怎么解决这个问题。任何帮助AP preciated。我还是新的反射所以这可能是一个简单的问题。
and have it return the number that I've input. But as I said illegal argument exception, and I'm not sure exactly how to solve this. Any help appreciated. I am still new to reflection so this might be a simple issue.
下面是错误日志:
别的东西,我忘了提的是,我用下面的code检查,这种方法确实存在在类文件:
Something else I forgot to mention is that I've used the following code to check that this method really exists in the class file:
Method[] checkMethod = classToInvestigate.getDeclaredMethods();
Test = new String[checkMethod.length];
int i = 0;
for(Method m : checkMethod)
{
// Found a method m
Test[i] = m.getName();
i++;
}
和方法setLine1Number在数组中返回。所以我相当有信心它是存在的,我能以某种方式使用它。
And the method setLine1Number was returned in the array. So I'm fairly confident it is there and I can use it somehow.
推荐答案
您需要的实例的类的你想操作的 - 这已经创造了类型的对象。
You need an instance of the class you wish to operate on - an object of that type that has already been created.
您传递给 setLine1Number.invoke()
例如,如果我们处理的整数
类,你会说:
For example, if we were dealing with the Integer
class you'd say:
Integer i = new Integer(5);
...
Object TestReturn = someIntegerMethod.invoke(i, arglist);
这篇关于如何正确使用反射来访问电话管理器隐藏方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!