forked from getActivity/AndroidProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckNetAspect.java
More file actions
53 lines (46 loc) · 1.59 KB
/
Copy pathCheckNetAspect.java
File metadata and controls
53 lines (46 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.hjq.demo.aop;
import android.app.Application;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import androidx.core.content.ContextCompat;
import com.hjq.demo.R;
import com.hjq.demo.manager.ActivityManager;
import com.hjq.toast.ToastUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
/**
* author : Android 轮子哥
* github : https://github.com/getActivity/AndroidProject
* time : 2020/01/11
* desc : 网络检测切面
*/
@Aspect
public class CheckNetAspect {
/**
* 方法切入点
*/
@Pointcut("execution(@com.hjq.demo.aop.CheckNet * *(..))")
public void method() {}
/**
* 在连接点进行方法替换
*/
@Around("method() && @annotation(checkNet)")
public void aroundJoinPoint(ProceedingJoinPoint joinPoint, CheckNet checkNet) throws Throwable {
Application application = ActivityManager.getInstance().getApplication();
if (application != null) {
ConnectivityManager manager = ContextCompat.getSystemService(application, ConnectivityManager.class);
if (manager != null) {
NetworkInfo info = manager.getActiveNetworkInfo();
// 判断网络是否连接
if (info == null || !info.isConnected()) {
ToastUtils.show(R.string.common_network_hint);
return;
}
}
}
//执行原方法
joinPoint.proceed();
}
}