xposed方法替换replaceHookedMethod有条件吗?

Is there a condition for xposed method replacement replaceHookedMethod?

我是 xposed 开发的新手,我被卡住了:

我 hook 一个方法,检查一些东西,然后我想决定是只用 return true; 替换它还是让它 运行。但是我还没有找到将条件设置为 replaceHookedMethod(..)

的可能性

我知道我可以在 afterHookedMethod 或 beforeHookedMethod 中设置 return 值,但这不会阻止该方法 运行ning.

这是我的简短示例:

private static boolean flag;

...

findAndHookMethod(Activity.class, "onKeyDown", int.class, KeyEvent.class, new XC_MethodHook() {
    @Override
    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
        //check some stuff here and set flag = true or flag = false
    }

    protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
        //this methode should only be called if the flag is true
        return true;
    }
};

有什么想法/建议吗? 提前致谢!

您可以使用 XC_MethodHookbeforeHookedMethod(..) 简单地实现您想要的:

如果在beforeHookedMethod(..)中调用param.setResult(..)param.setThrowable(..),则不会执行您hook的原始方法。

It's not too hard to guess that the are executed before/after the original method. You can use the "before" method to evaluate/manipulate the parameters of the method call (via param.args) and even prevent the call to the original method (sending your own result). https://github.com/rovo89/XposedBridge/wiki/Development-tutorial

我检查了 XC_MethodReplacement 的源代码,它证实了我在这个答案开头所做的陈述。它在内部扩展 XC_methodHook 并使用以下实现:

    protected final void beforeHookedMethod(MethodHookParam param) throws Throwable {
        try {
            Object result = replaceHookedMethod(param);
            param.setResult(result);
        } catch (Throwable t) {
            param.setThrowable(t);
        }
    }

因此如果要替换方法,只需检查beforeHookedMethod中的条件并设置结果即可。