Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

hook - How to get Context through hooking in android

The background is :

  1. I'm using xposed framework to hook a third party app.
  2. When I hook method XXX, xposed gave me "ClassNotFound" error. I checked and found the method XXX is in a dex file and would be loaded by DexClassLoader on the run.
  3. To hook the method XXX, I need to change the default ClassLoader in xposed to DexClassLoader. To get a DexClassLoader instance, I need a Context instance of the third party app.
  4. Here comes the question: how to get the context instance?

I searched stackoverflow and found someone said you can hook the method in Activity or Receiver to retrieve their context. But I checked the Activity.class and found no method that return Context type value, and only one method has Context type parameter, the onCreateView(String name, Context context, AttributeSet attrs).

Is there any way to get the Context?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...