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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐