在开发xposed插件的过程中遇到的一些问题及解决办法

Android 4.4以下版本出现ClassNotFound

主要是Xposed不支持4.4以下也就是Dalvik虚拟机multiDex

可使用以下办法:

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT_WATCH) {
    findAndHookMethod(Application.class, "dispatchActivityResumed", Activity.class, new XC_MethodHook() {
        @Override
        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
           XposedBridge.log("activity class:" + ((Activity) param.args[0]).getClass());
         }
     });
}

Android 4.4以下获取Context为null的问题

Android 4.4以下使用以下方法无法获取Context

if (loadPackageParam.packageName.equals("android")) {
        final Object activityThread = XposedHelpers.callStaticMethod(XposedHelpers.findClass("android.app.ActivityThread", null), "currentActivityThread");
        final Context systemContext = (Context) XposedHelpers.callMethod(activityThread, "getSystemContext");

因此使用以下方法寻找类中的Context变量获取

if (loadPackageParam.packageName.equals("android")) {
    final Class<?> phoneWindowManager;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        phoneWindowManager = XposedHelpers.findClass("com.android.server.policy.PhoneWindowManager", loadPackageParam.classLoader);
    } else {
        phoneWindowManager = XposedHelpers.findClass("com.android.internal.policy.impl.PhoneWindowManager", loadPackageParam.classLoader);
    }
    XposedBridge.hookAllMethods(phoneWindowManager, "interceptKeyBeforeQueueing", new XC_MethodHook() {

        @Override
        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
            int v1 = ((KeyEvent) param.args[0]).getKeyCode();
            Field contextField = XposedHelpers.findField(phoneWindowManager, "mContext");
            contextField.setAccessible(true);
            final Context context = (Context) contextField.get(param.thisObject);
        }
    });
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

破解高德SDK实现免key 上一篇
自定义Android-Studio工程模板 下一篇