Hook Activity的生命周期
2020-03-24 本文已影响0人
wislie_zhu
用Aop的方式打印当前Activity执行的生命周期,
首先追踪了Activity生命周期的调用到底来自何处, 在Activity的onCreate方法中瞧见
getApplication().dispatchActivityCreated(this, savedInstanceState);
去Application类中找dispatchActivityCreated方法
void dispatchActivityCreated(Activity activity, Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity,
savedInstanceState);
}
}
}
再去查找onActivityCreated方法,有种莫名的伤感
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
原来Application中有所有和Activity生命周期相关的方法, 以前怎么没注意,接下来该怎么做?
根据hook机制,我们得获取ActivityLifecycleCallbacks对象,并且需要创建持有ActivityLifecycleCallbacks对象的代理,最后将代理对象添加到ActivityLifecycleCallbacks集合中
一.获取ActivityLifecycleCallbacks对象
//获取到Application中的mActivityLifecycleCallbacks字段
Field activityLifecycleCallbacksField = Application.class.getDeclaredField("mActivityLifecycleCallbacks");
//保证访问权限
activityLifecycleCallbacksField.setAccessible(true);
//获取当前activity的application对象
Application application = activity.getApplication();
//和Activity生命周期相关的持有者集合,通过反射机制获得
ArrayList<Application.ActivityLifecycleCallbacks> callbacksList =
(ArrayList<Application.ActivityLifecycleCallbacks>) activityLifecycleCallbacksField.get(application);
//获取activityLifecycleCallbacksClass类
Class<?> activityLifecycleCallbacksClass = Class.forName("android.app.Application$ActivityLifecycleCallbacks");
//创建一个ActivityLifecycleCallbacks的实现对象
final VirtualActivityLifecycleCallbacks callbacks = new VirtualActivityLifecycleCallbacks();
二.创建持有ActivityLifecycleCallbacks对象的代理
//创建一个ActivityLifecycleCallbacks的代理对象
Object proxyActivityLifecycleCallbacks =
Proxy.newProxyInstance(activityLifecycleCallbacksClass.getClassLoader(),
new Class[]{activityLifecycleCallbacksClass},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(callbacks, args);
}
});
三.将代理对象添加到callbacksList集合中
callbacksList.add((Application.ActivityLifecycleCallbacks) proxyActivityLifecycleCallbacks);
完整代码如下
public static void hook(Activity activity) {
try {
//获取到Application中的mActivityLifecycleCallbacks字段
Field activityLifecycleCallbacksField = Application.class.getDeclaredField("mActivityLifecycleCallbacks");
//保证访问权限
activityLifecycleCallbacksField.setAccessible(true);
//获取当前activity的application对象
Application application = activity.getApplication();
//和Activity生命周期相关的持有者集合,通过反射机制获得
ArrayList<Application.ActivityLifecycleCallbacks> callbacksList =
(ArrayList<Application.ActivityLifecycleCallbacks>) activityLifecycleCallbacksField.get(application);
//获取activityLifecycleCallbacksClass类
Class<?> activityLifecycleCallbacksClass = Class.forName("android.app.Application$ActivityLifecycleCallbacks");
//创建一个ActivityLifecycleCallbacks的实现对象
final VirtualActivityLifecycleCallbacks callbacks = new VirtualActivityLifecycleCallbacks();
//创建一个ActivityLifecycleCallbacks的代理对象
Object proxyActivityLifecycleCallbacks =
Proxy.newProxyInstance(activityLifecycleCallbacksClass.getClassLoader(),
new Class[]{activityLifecycleCallbacksClass},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Log.i(TAG, activity.getClass().getSimpleName() + "---" + method.getName());
return method.invoke(callbacks, args);
}
});
//将代理对象添加到callbacksList集合中
callbacksList.add((Application.ActivityLifecycleCallbacks) proxyActivityLifecycleCallbacks);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 创建ActivityLifecycleCallbacks的实现类
*/
private static class VirtualActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
我们只需要在基类Activity的onCreate方法中调用
LifeCycleListenerHelper.hook(this);
这样就完成了,居然没答上来