【发布时间】:2012-02-20 22:51:19
【问题描述】:
我不确定我是否遇到了反射本身的问题,或者我尝试获取的方法。
我想做的是从类中调用函数 setLine1Number:
com.android.internal.telephony.gsm.GSMPhone
这样我就可以将我的号码正确插入手机,因为它不需要在我的 SIM 卡中。因此,我希望能够调用函数 getLine1Number 并让它返回我设置的相同数字。
反射似乎是能够使用此功能的唯一方法,因为它不在公共 API 中。
我已经写了这个,但不断收到非法参数异常。这是我的代码:
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.
现在,我想打电话给我
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String PhoneNumber2 = telephonyManager.getLine1Number();
并让它返回我输入的数字。但正如我所说的非法参数异常,我不确定如何解决这个问题。任何帮助表示赞赏。我对反思还是陌生的,所以这可能是一个简单的问题。
这是错误日志:
还有一点我忘了提的是,我使用下面的代码来检查这个方法是否真的存在于类文件中:
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 方法。所以我相当有信心它就在那里,我可以以某种方式使用它。
【问题讨论】:
标签: java android reflection telephony illegalargumentexception