java里的反射如下:
一。写个类
public class Simple {
private void displayMessage(String strMsg) {
System.out.println("message is:" + strMsg);
}
}
二。反射调用
import java.lang.reflect.*;
public class Test {
/**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchMethodException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
Class simpleClass = Class.forName("Simple");
Object simpelObject = simpleClass.newInstance();
Class[] args1 = new Class[1];
args1[0] = String.class;
Method simpleMethod = simpleClass.getDeclaredMethod("displayMessage",
args1);
simpleMethod.setAccessible(true);
String[] argments = new String[1];
argments[0] = "Hello,world";
simpleMethod.invoke(simpelObject, argments);
}
}
Class<?> smsManager = Class.forName("android.telephony.SmsManager" );
Object obj_smsManager=smsManager.getClass().forName("android.telephony.SmsManager").getMethod("getDefault",null).invoke(null,null);
((SmsManager) obj_smsManager).sendTextMessage("***",null,"test",null,null);
这篇博客介绍了如何通过Java反射机制调用Android系统的隐藏API,首先展示了创建一个简单的类并进行反射调用的方法,然后演示了如何获取并使用`SmsManager`类,特别是调用`getDefault`方法并发送短信的步骤。
3307

被折叠的 条评论
为什么被折叠?



