Xposed hook acitivity并获取对应的参数等信息
本篇文章主要讲述下使用Xposed hook activity 的oncreate 方法,从而获取到activity 对象,来打印输出intent 的相关内容.
1: hook onCreate()
代码如下:打印activity.
XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Activity activity = (Activity) param.thisObject;
String activityName = activity.getClass().getName();
Log.d("IntentHook", "Activity created: " + activityName);
}
});
2: 打印intent参数
具体的代码如下:
package com.zh.xpose;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.function.BiConsumer;
import static com.zh.xpose.HookModule.TAG;
/**
* @Author: zh
* @Time: 23-12-6.
* @Email: zhaohang@zhizhangyi.com
* @Describe:
*/
public class Utils {
public static Method getDeclaredMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) {
while(true) {
try {
if (cls != Object.class) {
try {
Method m = cls.getDeclaredMethod(methodName, parameterTypes);
if (!m.isAccessible()) {
m.setAccessible(true);
}
return m;
} catch (Throwable throwable) {
cls = cls.getSuperclass();
continue;
}
}
} catch (Throwable throwable) {
Log.e(TAG, "getDeclaredMethod: ",throwable );
}
return null;
}
}
@TargetApi(Build.VERSION_CODES.N)
public static void printIntentExtras(Intent i, final String tag) {
try {
if (null != i.getExtras()) {
Bundle extras = i.getExtras();
Method getMapMtd = getDeclaredMethod(Bundle.class, "getMap");
ArrayMap<String, Object> mapList;
if (null != getMapMtd) {
mapList = (ArrayMap<String, Object>) getMapMtd.invoke(extras);
} else {
mapList = (ArrayMap<String, Object>)getFieldValue(extras.getClass(),extras, "mMap");
}
if (null != mapList) {
mapList.forEach(new BiConsumer<String, Object>() {
@Override
public void accept(String s, Object o) {
Log.d(tag, "ams: intent extra " + s + " = " + o);
if (o instanceof Intent) {
printIntentExtras((Intent) o, tag);
}
}
});
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
public static Object getFieldValue(Class<?> cls, Object org, String name) {
try {
Field f = getDeclaredField(cls, name);
if (null != f) {
return f.get(org);
}
} catch (Throwable throwable) {
Log.e(TAG, "getFieldValue: ", throwable);
}
return null;
}
public static Field getDeclaredField(Class<?> cls, String fieldName) {
if (null != cls) {
while (cls != Object.class) {
try {
Field f = cls.getDeclaredField(fieldName);
if (!f.isAccessible()) {
f.setAccessible(true);
}
return f;
} catch (Throwable var3) {
cls = cls.getSuperclass();
}
}
}
return null;
}
}
运行可已看到输出如下:
2023-10-04 07:32:13.415 1760-1760/com.zh.xpose D/IntentHook: Activity created: com.zh.xpose.SettingActivity
2023-10-04 07:32:27.870 2005-2005/com.tencent.wework D/IntentHook: hook: intent start
2023-10-04 07:32:28.426 2005-2005/com.tencent.wework D/IntentHook: Activity created: com.tencent.wework.launch.LaunchSplashActivity
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra start_activity_trace_flag = 1
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra extra_login_by_ww_code =
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra activity_request_code = -1
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra extra_show_logo_animation = true
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra popupAnimation = true
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra start_activity_trace_ignore = true
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: ams: intent extra extra_logout_type = 0
2023-10-04 07:32:28.586 2005-2005/com.tencent.wework D/IntentHook: Activity created: com.tencent.wework.login.controller.LoginWxAuthActivity
2023-10-04 07:32:28.823 2136-2136/? D/IntentHook: hook: intent start
2023-10-04 07:32:29.231 2213-2213/? D/IntentHook: hook: intent start
RecommendUserDialogFragment