Android xposed: how do I get the context of a hooked application?

Like the title, I want to hook up third applications and call my application activities from third application activities, so I want to get the context of third applications. What should I do?

The package of the third application is com.ss.android.gallery.heavy, and the package of my project is com.example.hao.hookstartupinterfacetest;

My xposed module.java (mainactivity is the activity of my project)

XposedHelpers.findAndHookMethod("com.ss.android.gallery.heavy.activity.SplashActivity",
                loadPackageParam.classLoader, "getMainIntent", new XC_MethodReplacement() {
                    @Override
                    protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {
                        Context context = (Activity)methodHookParam.getResult();
                        return new Intent(context, MainActivity.class);
                    }
                });

Splashactivity.class (in the third application, mainactivity belongs to the third application here.)

   public class SplashActivity extends BaseSplashActivity{
       protected Intent getMainIntent(){
           return new Intent(this, MainActivity.class);
       }
   }

error

01-18 20:38:23.669 4730-4730/com.ss.android.gallery.heavy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ss.android.gallery.heavy, PID: 4730
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ss.android.gallery.heavy/com.example.hao.hookstartupinterfacetest.MainActivity}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
        at android.app.Activity.startActivityForResult(Activity.java:3745)
        at android.app.Activity.startActivityForResult(Activity.java:3706)
        at android.app.Activity.startActivity(Activity.java:4016)
        at android.app.Activity.startActivity(Activity.java:3984)
        at com.ss.android.gallery.base.activity.BaseSplashActivity.goMainActivity(BaseSplashActivity.java:61)
        at com.ss.android.gallery.base.activity.BaseSplashActivity.access$000(BaseSplashActivity.java:17)
        at com.ss.android.gallery.base.activity.BaseSplashActivity$1.run(BaseSplashActivity.java:76)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

resolvent:

You are trying to directly use the context of the hook application to start the activity of the application. Because the activity is not actually part of the hook application, the operation does not work (so the error "not defined in androidmanifest. XML" appears). When creating the intention, you must explicitly specify the full name of the application package and class:

Intent intent = new Intent();
intent.setClassName(
    // Your app's package name
    "com.example.hao.hookstartupinterfacetest",
    // The full class name of the activity you want to start
    "com.example.hao.hookstartupinterfacetest.MainActivity");
return intent;

You should also set the Android: exported property of mainactivity to true in androidmanifest.xml so that it can be started through a hooked application

In addition, context = (activity) methodhookparam. Getresult(); It should be context context = (activity) methodhookparam.thisobject;, Although you don't actually need to use this method at all

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>