4

Like the title, I want to hook a third app, and can call my application activity from the third app's activity.So I want to get the third app's context, how should I do?

The third app's package is com.ss.android.gallery.heavy, My project's package is com.example.hao.hookstartupinterfacetest;

My Xposed Module.java(MainActivity here is my project's activity)

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

The SplashActivity.class (It is in the third app, MainActivity here belongs to the third app.)

   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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hao
  • 57
  • 1
  • 5
  • [AndroidAppHelper.currentApplication](http://api.xposed.info/reference/android/app/AndroidAppHelper.html#currentApplication()) – Andrew Sun Jan 18 '17 at 03:19
  • Hi, you are great, I solve the problem, but it cannot call my app's activity.Do you know the principle? – hao Jan 18 '17 at 11:51
  • What are you trying to do? Please edit your question and add the code and any errors that you are having problems with. – Andrew Sun Jan 18 '17 at 14:50
  • I'm going to guess you're trying to start your activity using the application context. See this: http://stackoverflow.com/questions/9237448/how-to-start-an-intent-if-context-is-not-activity-context-but-application-contex – Andrew Sun Jan 18 '17 at 14:54

2 Answers2

2

You are trying to start your app's activity directly using the context of the hooked app, which doesn't work since that activity is not actually part of the hooked app (hence the "not defined in AndroidManifest.xml" error). You must explicitly specify your app package and the full name of the class when you create your intent:

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 your MainActivity to true inside your AndroidManifest.xml, so that it can be started by the hooked app.

Also, Context context = (Activity)methodHookParam.getResult(); should probably be Context context = (Activity)methodHookParam.thisObject;, though you don't actually need it at all with this method.

Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
2

if you want to start a activity from a hooked method you can use the following code.

Note: The answer is based on my understanding of question ( I did not understand English from the question ). Please comment below before down voting.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.hao.hookstartupinterfacetest", "com.example.hao.hookstartupinterfacetest.MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AndroidAppHelper.currentApplication().startActivity(intent);

if you want to get context of third party app. You can cast the param.thsobject to Activity.

Activity activity = (Activity) param.thisobject;

then you can use

activity.getApplicationContext();

make sure that the class ( which you are hooking ) is an instance of Activity otherwise you cant cast param.thisobject to Activity. Hope it helps.

Suraj
  • 184
  • 1
  • 14