package com.xxx.base.aspectj;
import android.util.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import java.util.HashMap;
import java.util.Map;
/**
* author: liweipeng
* date: 2020-04-09
*/
@Aspect
public class ActivityTimeAspectJ {
private Map<String, Long> timeMap = new HashMap<>();
@Pointcut("execution(* android.app.Activity+.onCreate(..))")
public void activityOnCreate() {
}
@Pointcut("execution(* android.app.Activity+.onResume(..))")
public void activityOnResume() {
}
@Pointcut("execution(* android.support.v4.app.Fragment+.onCreate(..))")
public void fragmentOnCreate() {
}
@Pointcut("execution(* android.support.v4.app.Fragment+.onResume(..))")
public void fragmentOnResume() {
}
@Before("activityOnCreate()||fragmentOnCreate()")
public void activityOnCreateBefore(JoinPoint joinPoint) {
String name = joinPoint.getThis().getClass().getName();
Log.e("aspectj", "aspectj---onCreate,name=" + name);
long startTime = System.currentTimeMillis();
timeMap.put(name, startTime);
}
@After("activityOnResume()||fragmentOnResume()")
public void activityOnOnResumeAfter(JoinPoint joinPoint) {
String name = joinPoint.getThis().getClass().getName();
Log.e("aspectj", "aspectj---onResume,name=" + name);
long endTime = System.currentTimeMillis();
if (timeMap.containsKey(name)) {
Long startTime = timeMap.remove(name);
Log.e("aspectj", "aspectj---onResume---" + (endTime - startTime)+"ms--"+name);
}
}
// @Around("onCreateOrOnResume()")
// public Object onCreateOrOnResumeAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// Object result = null;
// try {
// AppCompatActivity activity = (AppCompatActivity) proceedingJoinPoint.getTarget();
// // Log.d("AJAOP", "Aop Info" + activity.getClass().getCanonicalName() +
// // "\r\nkind : " + thisJoinPoint.getKind() +
// // "\r\nargs : " + thisJoinPoint.getArgs() +
// // "\r\nClass : " + thisJoinPoint.getClass() +
// // "\r\nsign : " + thisJoinPoint.getSignature() +
// // "\r\nsource : " + thisJoinPoint.getSourceLocation() +
// // "\r\nthis : " + thisJoinPoint.getThis()
// // );
// long startTime = System.currentTimeMillis();
// result = proceedingJoinPoint.proceed();
// String activityName = activity.getClass().getCanonicalName();
//
// Signature signature = proceedingJoinPoint.getSignature();
// String sign = "";
// String methodName = "";
// if (signature != null) {
// sign = signature.toString();
// methodName = signature.getName();
// }
// if (!TextUtils.isEmpty(activityName) && !TextUtils.isEmpty(sign) && sign.contains(activityName)) {
// Log.d("", "aspectj---activityName=" + activityName + ",methodName=" + methodName + ",sign=" + sign);
// }
// } catch (Exception e) {
// e.printStackTrace();
// } catch (Throwable throwable) {
// throwable.printStackTrace();
// }
// return result;
// }
}
2021-06-23 ActivityTimeAspectJ打点计时记录
最新推荐文章于 2022-01-06 07:50:00 发布
本文介绍了一个使用AspectJ实现的Activity及Fragment生命周期监控方案。通过AOP技术,在Activity和Fragment的onCreate及onResume方法中记录时间戳,计算并记录执行时间,帮助开发者更好地了解各个组件的启动效率。
3087

被折叠的 条评论
为什么被折叠?



