Android-AOP-生命周期无侵入记录

引入插件

classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.8'

应用插件

apply plugin: 'android-aspectjx'

引入库

  implementation 'org.aspectj:aspectjrt:1.8.14'

在这里插入图片描述

代码实现:
ActivityAop.java

@Aspect
public class ActivityAop {

    private static final String TAG = ActivityAop.class.getSimpleName();


      @Around("execution(@com.crobot.basic.activity_life_cycle.LifeCycleLog * *(..))")
    public void addLifeCycleLogPointcut(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        LifeCycleLog addLog = signature.getMethod().getAnnotation(LifeCycleLog.class);
        if (addLog != null) {
            Object target = joinPoint.getTarget();
            String className = "";
            if (target != null) {
                className = target.getClass().getSimpleName();
            }
            Log.d(TAG, "" + className + "-" + signature.getMethod().getName()+"obj:"+target.toString());
            joinPoint.proceed();
//            Log.d(TAG, "end execute:" + className + "-" + signature.getMethod().getName());
        } else {
            joinPoint.proceed();
        }
    }
}

BaseActivity.java

public class BaseActivity extends AppCompatActivity {
    @LifeCycleLog
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @LifeCycleLog
    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @LifeCycleLog
    @Override
    protected void onStart() {
        super.onStart();
    }


    @LifeCycleLog
    @Override
    protected void onResume() {
        super.onResume();
    }

    @LifeCycleLog
    @Override
    protected void onPause() {
        super.onPause();
    }

    @LifeCycleLog
    @Override
    protected void onStop() {
        super.onStop();
    }

    @LifeCycleLog
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @LifeCycleLog
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }
}

实现效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值