android Hook IActivityManager和IActivityTaskManager
public class HookManager {private static final String TAG = "HookManager";public static void hookActivityManager(){if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P){hookIActivityTaskManager();}else
·
public class HookManager {
private static final String TAG = "HookManager";
public static void hookActivityManager(){
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P){
hookIActivityTaskManager();
}else{
hookIActivityManager();
}
}
/**
* android 10以上
*/
private static void hookIActivityTaskManager() {
try{
Field iActivityTaskManagerFiled = null;
Class<?> activityTaskManagerCls = Class.forName("android.app.ActivityTaskManager");
iActivityTaskManagerFiled = activityTaskManagerCls.getDeclaredField("IActivityTaskManagerSingleton");
iActivityTaskManagerFiled.setAccessible(true);
Object singleton = iActivityTaskManagerFiled.get(null);
Class<?> singletonClass = Class.forName("android.util.Singleton");
Field mInstanceField = singletonClass.getDeclaredField("mInstance");
mInstanceField.setAccessible(true);
final Object IActivityTaskManager = mInstanceField.get(singleton);
Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader()
, new Class[]{Class.forName("android.app.IActivityTaskManager")}
, new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
//todo 拦截方法
return method.invoke(IActivityTaskManager, args);
}
});
mInstanceField.set(singleton, proxy);
Log.d(TAG, "hook activity task manager success");
}catch (Exception e){
e.printStackTrace();
}
}
/**
* android 10以下
*/
private static void hookIActivityManager() {
try{
Field singletonField = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Class<?> actvityManagerCLs = Class.forName("android.app.ActivityManager");
singletonField = actvityManagerCLs.getDeclaredField("IActivityManagerSingleton");
} else {
Class<?> actvityManagerNativeCls = Class.forName("android.app.ActivityManagerNative");
singletonField = actvityManagerNativeCls.getDeclaredField("gDefault");
}
singletonField.setAccessible(true);
Object singleton = singletonField.get(null);
Class<?> actvityManagerCls = Class.forName("android.util.Singleton");
Field mInstanceField = actvityManagerCls.getDeclaredField("mInstance");
mInstanceField.setAccessible(true);
final Object iActivityManager = mInstanceField.get(singleton);
Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader()
, new Class[]{Class.forName("android.app.IActivityManager")}
, new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
//todo 拦截方法
return method.invoke(iActivityManager, args);
}
});
mInstanceField.set(singleton, proxy);
Log.d(TAG, "hook activity manager success");
}catch (Exception e){
e.printStackTrace();
}
}
}
适配Android 7, 8, 9, 10
更多推荐
已为社区贡献3条内容
所有评论(0)