通过广播接收器调用方法

时间:2015-05-29 01:37:25

标签: android broadcastreceiver invoke xposed-framework

我想在运行时使用Xposed在我的应用程序中调用系统方法。我认为广播接收器应该是正确的方式。

如何在正确的上下文中设置广播接收器,以便能够调用该方法?我是否需要某种代理实例?一个简短的示例代码会很好:)

1 个答案:

答案 0 :(得分:0)

我也试过这个。您可以使用反射来执行此操作。

示例:

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);

我不知道如何在运行时为所有应用程序挂钩方法,因为我发现每个应用程序都有不同的类对象的不同xposed模块实例。他们不互相分享对象。 (如果性能不好,我不想使用数据库或磁盘文件)