我想在运行时使用 Xposed 从我的应用程序调用系统方法。我认为广播接收器应该是正确的方法。
如何在正确的上下文中设置广播接收器以便能够调用该方法?我需要该类的某种代理实例吗?一个简短的示例代码会很好:)
我想在运行时使用 Xposed 从我的应用程序调用系统方法。我认为广播接收器应该是正确的方法。
如何在正确的上下文中设置广播接收器以便能够调用该方法?我需要该类的某种代理实例吗?一个简短的示例代码会很好:)
我也试过这个。您可以使用反射来执行此操作。
例子:
在 com.your.package.app.XModuleBridge
public static abstract class HandleHook {
public abstract void before(Object paramObj);
public abstract void after(Object paramObj);
}
public boolean unhook(Object key) {
return false;
}
/* handle must be instance of HandleHook */
public Object hook(Method method, Object handle) {
return null;
}
在 Xposed 模块中。
/* bridge = Class object of XModuleBridge */
XposedBridge.hookMethod(bridge.getDeclaredMethod("hook",
Method.class, Object.class), new XC_MethodHook() {
{
System.out.println(">>>>> some log ");
}
@Override
protected void afterHookedMethod(MethodHookParam param)
throws Throwable {
param.setResult(hookMethod(param));
}
});
XposedBridge.hookMethod(
bridge.getDeclaredMethod("unhook", Object.class),
new XC_MethodHook() {
{
System.out.println(">>>>> some log ");
}
@Override
protected void afterHookedMethod(MethodHookParam param)
throws Throwable {
XC_MethodHook.Unhook hook = hookedMethod
.remove(param.args[0]);
if (hook != null) {
hook.unhook();
}
param.setResult(true);
}
});
--
private Object hookMethod(XC_MethodHook.MethodHookParam param) {
final Object[] args = param.args;
// TODO check args.
XC_MethodHook.Unhook unhook = XposedBridge.hookMethod((Method) args[0],
new XC_MethodHook() {
private Object call = args[1];
{
System.out.println(">>>>> some log ");
}
@Override
protected void beforeHookedMethod(MethodHookParam param)
throws Throwable {
call.getClass()
.getDeclaredMethod("before", Object.class)
.invoke(call, param);
}
@Override
protected void afterHookedMethod(MethodHookParam param)
throws Throwable {
call.getClass()
.getDeclaredMethod("after", Object.class)
.invoke(call, param);
}
});
Object key = new Object();
hookedMethod.put(key, unhook);
return key;
}
用法:
XModuleBridge bridge = new XModuleBridge();
Method method = null;
try {
method = textView.getClass().getDeclaredMethod("append",
CharSequence.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Object hooked = bridge.hook(method, new HandleHook() {
@Override
public void before(Object paramObj) {
/* paramObj is instance of XC_MethodHook.MethodHookParam */
/* reflect paramObj in ParamConverter.
or cast to XC_MethodHook.MethodHookParam*/
ParamConverter param = new ParamConverter(
paramObj);
System.out.println(">>>>>>> 1" + param.args[0]);
param.args[0] = "changed.";
System.out.println(">>>>>>> 2" + param.args[0]);
}
@Override
public void after(Object paramObj) {}
});
// ...
bridge.unhook(hooked);
我不知道如何在运行时为所有应用程序挂钩方法,因为我发现每个应用程序都有不同 Class 对象的不同 xposed 模块实例。他们彼此不共享对象。(如果性能不好,我不想使用数据库或磁盘文件)