android反射的应用

http://www.java2s.com/Code/Java/Reflection/MethodReflection.htm
http://almondmendoza.com/2009/11/27/calling-private-methods-in-android/
http://tutorials.jenkov.com/java-reflection/index.html
First and foremost, I won’t be held responsible for the content of the article if applied to your apps, and if it results in any kinds of misfunctions and such. Use it at your own risk and use it when there’s are no other way that is available in android.

In java, there is a set of API called Reflection API, see this for more detailed look and this for examples. What Reflection does is that it will, from its name, reflect the class for you, reflecting public, private, protected, static and even native variables, function/methods, classes and more. By getting their reflection you could execute private functions or get private class. In this article however, will focus on calling private methods on a public class. We would use SmsManager and test it on Android 1.6 throughout this article

try {
// the following would just be (but using reflections)
// sm.sendTextMessage("<your number>", null, "Test", null, null);

// initialize variables
SmsManager sm = SmsManager.getDefault();
Class<?> c = sm.getClass();

// debug, print all methods in this class , there are 2 private methods in this class
Method[] ms = c.getDeclaredMethods();
for (int i = 0; i < ms.length; i++)
Log.d("ListMethos",ms[i].toString());

// get private method 1
Method m1 = SmsManager.class.getDeclaredMethod("createMessageListFromRawRecords", List.class);
Log.d("success","success getting sendRawPdu");

// get private method 2
byte[] bb = new byte[1];
Method m2 = SmsManager.class.getDeclaredMethod("sendRawPdu",bb.getClass(),bb.getClass(),
PendingIntent.class,PendingIntent.class);
Log.d("success","success getting sendRawPdu");

// send message
m2.setAccessible(true);
SmsMessage.SubmitPdu pdus = SmsMessage.getSubmitPdu( null,"<your number>", "Test", false);
m2.invoke(sm, pdus.encodedScAddress, pdus.encodedMessage, null, null );

Log.d("success","success sedding message");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Explanation
Get the private method from the SmsManager class having one argument of List
Method m1 = SmsManager.class.getDeclaredMethod(“createMessageListFromRawRecords”, List.class);
– From our trace: D/ListMethos( 2900): private java.util.ArrayList android.telephony.SmsManager.createMessageListFromRawRecords(java.util.List)

Get another method from SmsManager that has 4 argument
Method m2 = SmsManager.class.getDeclaredMethod(“sendRawPdu”,bb.getClass(),bb.getClass(), PendingIntent.class,PendingIntent.class);
– From our trace: D/ListMethos( 2900): private void android.telephony.SmsManager.sendRawPdu(byte[],byte[],android.app.PendingIntent,android.app.PendingIntent)

Make the private function to be public
m2.setAccessible(true);

Call the method m2(sendRawPdu) on SmsManager object with the following parameters
m2.invoke(sm, pdus.encodedScAddress, pdus.encodedMessage, null, null );

Basically thats it Hope this helps and remember limit the use of this, its not recommended by all means
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值