How to get Context through hooking in android

感情迁移 提交于 2019-11-29 15:24:12

The answer from the poster below is more succinct:

Context context = (Context) AndroidAppHelper.currentApplication();

An alternative hack is to retrieve the current activity (which can be cast to Context) like this:

Class<?> instrumentation = XposedHelpers.findClass(
                "android.app.Instrumentation", lpparam.classLoader);

XposedBridge.hookAllMethods(instrumentation, "newActivity", new XC_MethodHook() {

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {

                    mCurrentActivity = (Activity) param.getResult();

                    Log.v(TAG, "Current Activity : " + mCurrentActivity.getClass().getName());
                }
});

Regarding the class loader, if it consists of the main app classloader then you can retrieve it from LoadPackageParam passed to the handleLoadPackage method.

If the app itself creates a new DexClassLoader then you can hook the DexClassLoader constructor to keep a reference to it. That way you have the actual ClassLoader that contains your class and method. No need to get any context.

Xposed already gives you the current hooked apps class loader.

public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
        if (!lpparam.packageName.equals("com.android.systemui"))
            return;

        findAndHookMethod("com.android.systemui.statusbar.policy.Clock", lpparam.classLoader, "updateClock", new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                // this will be called before the clock was updated by the original method
            }
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                // this will be called after the clock was updated by the original method
            }
    });
    }

As you can see "lpparam.classLoader" is the current apps class loader. I think you may just have typed something incorrectly, are you able to post the code? But you can get the current hooked apps application intense witch can be casted to context. Context context = (Context) AndroidAppHelper.currentApplication();

Source:https://github.com/rovo89/XposedBridge/blob/master/src/android/app/AndroidAppHelper.java#L131 Source: https://github.com/rovo89/XposedBridge/wiki/Development-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!