Android AOP 之 AspectJ 统一处理网络检测

利用 AOP 之 Eclipse AspectJ

//采用 AspectJX 来快速配置 Eclipse AspectJ
//project gradle
dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        //add
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
plugins {
    id 'com.android.application'
    //add
    id 'android-aspectjx'
}
//module gradle
dependencies {
    //add
    implementation 'org.aspectj:aspectjrt:1.9.5'
}

定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckNetwork {
    String tip() default "未连接网络";
}

定义切面类

@Aspect
public class CheckNetworkAspect {
    private static final String TAG = "CheckNetworkAspect";

    @Pointcut("execution(@com.louisgeek.aop.CheckNetwork * *(..))")
    public void pointcutCheckNetwork() {
        Log.e(TAG, "pointcutCheckNetwork: ");
    }

    @Around("pointcutCheckNetwork()")
    public Object aroundCheckNetwork(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        CheckNetwork checkNetwork = signature.getMethod().getAnnotation(CheckNetwork.class);
        if (checkNetwork == null) {
            return null;
        }
        Context context = AspectTool.getContext(joinPoint.getThis());
        if (context == null) {
            return null;
        }
        String tip = checkNetwork.tip();
        if (TextUtils.isEmpty(tip)) {
            tip = "连接网络异常";
        }
        if (NetworkTool.isConnected(context)) {
            Log.e(TAG, "aroundCheckNetwork: 网络已连接 ");
            joinPoint.proceed();
        } else {
            Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();

        }
        return null;
    }
}

使用方式

id_tv_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: ");
                checkNetworkTest();
            }
});
        //
@CheckNetwork(tip = "网络开了小差")
public void checkNetworkTest() {
        Toast.makeText(this, "继续执行", Toast.LENGTH_SHORT).show();
}     

封装成 library 使用

1 project gradle

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
}

2 project gradle

 dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
		//add
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'

}

3 module gradle

plugins {
    id 'com.android.application'
    //add
    id 'android-aspectjx'
}

4 module gradle [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hja7xl3w-1616157703263)(https://jitpack.io/v/louisgeek/LG_AOP.svg)]

dependencies {
	...
	//add 
    implementation 'com.github.louisgeek:LG_AOP:x.x.x'
}
  • 使用方式与上面一致
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

louisgeek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值