如何用反射调SDK中被hide的函数??

Zack4zhou
  • 319

目的:
调用TelephonyManager中的如下函数(注意到参数int,不带参数没有被hide),我想读取副卡的信息

clipboard.png

尝试:
根据网络中的教程

Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Method method = telephonyManagerClass.getMethod("getSimState", new Class[]{int.class});
                    method.setAccessible(true);
                    Object object = method.invoke(telephonyManager,0);
                    int a = (int)object;
                    System.out.println(a);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }catch (Exception e){

                }

但是报错

java.lang.NoSuchMethodException: getSimState [int]
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at java.lang.Class.getConstructorOrMethod(Class.java:472)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at java.lang.Class.getMethod(Class.java:857)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at com.zjipst.zhouxiong.simstate.activity.MainActivity$2.onClick(MainActivity.java:61)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at android.view.View.performClick(View.java:4438)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at android.view.View$PerformClick.run(View.java:18442)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at android.os.Looper.loop(Looper.java:136)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5195)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
03-25 11:18:52.218 15812-15812/com.zjipst.zhouxiong.simstate W/System.err:     at dalvik.system.NativeStart.main(Native Method)
回复
阅读 4.9k
3 个回答
✓ 已被采纳

因为sdk中并没有getSimState(int slotIdx)方法,getSimState(int slotIdx)属于framework层TelephonyManager内hide方法,应用层反射应该是调不到的,我没有具体去调过,但AIDL或许你可以尝试一下。

反射是可以调用到hide方法,你应该去看看你自己的SDK中getSimState()方法的参数究竟是不是int

指定好类的包名,反射是可以获取到hide方法的,不过在程序中较消耗性能

宣传栏