Xposed问题笔记

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

Android 4.4以下版本出现ClassNotFound

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

可使用以下办法:

1
2
3
4
5
6
7
8
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

1
2
3
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变量获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
}
});
}
蔡小木 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!