用Aop的方式打印當(dāng)前Activity執(zhí)行的生命周期,
首先追蹤了Activity生命周期的調(diào)用到底來(lái)自何處, 在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);
}
原來(lái)Application中有所有和Activity生命周期相關(guān)的方法, 以前怎么沒注意,接下來(lái)該怎么做?
根據(jù)hook機(jī)制,我們得獲取ActivityLifecycleCallbacks對(duì)象,并且需要?jiǎng)?chuàng)建持有ActivityLifecycleCallbacks對(duì)象的代理,最后將代理對(duì)象添加到ActivityLifecycleCallbacks集合中
一.獲取ActivityLifecycleCallbacks對(duì)象
//獲取到Application中的mActivityLifecycleCallbacks字段
Field activityLifecycleCallbacksField = Application.class.getDeclaredField("mActivityLifecycleCallbacks");
//保證訪問(wèn)權(quán)限
activityLifecycleCallbacksField.setAccessible(true);
//獲取當(dāng)前activity的application對(duì)象
Application application = activity.getApplication();
//和Activity生命周期相關(guān)的持有者集合,通過(guò)反射機(jī)制獲得
ArrayList<Application.ActivityLifecycleCallbacks> callbacksList =
(ArrayList<Application.ActivityLifecycleCallbacks>) activityLifecycleCallbacksField.get(application);
//獲取activityLifecycleCallbacksClass類
Class<?> activityLifecycleCallbacksClass = Class.forName("android.app.Application$ActivityLifecycleCallbacks");
//創(chuàng)建一個(gè)ActivityLifecycleCallbacks的實(shí)現(xiàn)對(duì)象
final VirtualActivityLifecycleCallbacks callbacks = new VirtualActivityLifecycleCallbacks();
二.創(chuàng)建持有ActivityLifecycleCallbacks對(duì)象的代理
//創(chuàng)建一個(gè)ActivityLifecycleCallbacks的代理對(duì)象
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);
}
});
三.將代理對(duì)象添加到callbacksList集合中
callbacksList.add((Application.ActivityLifecycleCallbacks) proxyActivityLifecycleCallbacks);
完整代碼如下
public static void hook(Activity activity) {
try {
//獲取到Application中的mActivityLifecycleCallbacks字段
Field activityLifecycleCallbacksField = Application.class.getDeclaredField("mActivityLifecycleCallbacks");
//保證訪問(wèn)權(quán)限
activityLifecycleCallbacksField.setAccessible(true);
//獲取當(dāng)前activity的application對(duì)象
Application application = activity.getApplication();
//和Activity生命周期相關(guān)的持有者集合,通過(guò)反射機(jī)制獲得
ArrayList<Application.ActivityLifecycleCallbacks> callbacksList =
(ArrayList<Application.ActivityLifecycleCallbacks>) activityLifecycleCallbacksField.get(application);
//獲取activityLifecycleCallbacksClass類
Class<?> activityLifecycleCallbacksClass = Class.forName("android.app.Application$ActivityLifecycleCallbacks");
//創(chuàng)建一個(gè)ActivityLifecycleCallbacks的實(shí)現(xiàn)對(duì)象
final VirtualActivityLifecycleCallbacks callbacks = new VirtualActivityLifecycleCallbacks();
//創(chuàng)建一個(gè)ActivityLifecycleCallbacks的代理對(duì)象
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);
}
});
//將代理對(duì)象添加到callbacksList集合中
callbacksList.add((Application.ActivityLifecycleCallbacks) proxyActivityLifecycleCallbacks);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 創(chuàng)建ActivityLifecycleCallbacks的實(shí)現(xiàn)類
*/
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方法中調(diào)用
LifeCycleListenerHelper.hook(this);
這樣就完成了,居然沒答上來(lái)