悲剧就是把有价值的东西毁灭给人看。------鲁迅
其实原理就是一个list
,使用AOP
在创建时添加,销毁时移除
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| hljs java package com.ruben.utils; import android.app.Activity; import android.os.Build; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; /** * @ClassName: MyActivityManager * @Description: 我还没有写描述 * @Date: 2020/10/18 0018 10:16 * * * @author: <achao1441470436@gmail.com> * @version: 1.0 * @since: JDK 1.8 */ public class MyActivityManager { public static final List<Activity> activityList = Collections.synchronizedList(new LinkedList<>()); public static void addActivity(Activity activity) { activityList.add(activity); } public static void removeActivity(Activity activity) { activityList.remove(activity); } public static Activity getActivity() { if (isActivityEmpty()) { return null; } return activityList.get(activityList.size() - 1); } public static void killActivity() { killActivity(getActivity()); } public static void killActivity(Activity activity) { if (activity == null) { return; } if (isActivityEmpty()) { return; } activity.finish(); removeActivity(activity); } public static void killActivity(Class<?> cls) { Activity activity = getActivity(cls); if (activity == null) { return; } killActivity(activity); } public static Activity getActivity(Class<?> cls) { if (isActivityEmpty()) { return null; } if (cls == null) { return null; } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { return null; } List<Activity> activities = activityList.stream().filter(activity -> activity.getClass().equals(cls)).collect(Collectors.toList()); if (activities.isEmpty()) { return null; } return activities.get(0); } public static Activity getTopActivity() { Activity activity = null; synchronized (activityList) { final int index = activityList.size() - 1; if (index < 0) { return null; } activity = activityList.get(index); } return activity; } public static String getTopActivityName() { Activity topActivity = getTopActivity(); if (topActivity == null) { return null; } return topActivity.getClass().getName(); } public static void killAllActivity() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { return; } activityList.forEach(Activity::finish); activityList.clear(); } private static boolean isActivityEmpty() { return activityList.isEmpty(); } }
|
然后上篇博客配了Aspectj
这里就直接使用
|---------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 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
| hljs java package com.ruben.aop; import android.app.Activity; import android.app.Application; import android.util.Log; import com.ruben.manager.MyActivityLifecycleCallbacks; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import lombok.SneakyThrows; /** * @ClassName: MyActivityManagerAop * @Description: 我还没有写描述 * @Date: 2020/10/18 0018 11:40 * * * @author: <achao1441470436@gmail.com> * @version: 1.0 * @since: JDK 1.8 */ @Aspect public class MyActivityManagerAop { @Pointcut("execution(* com.ruben..*.onCreate(..))") public void onCreatePointcut() { } @SneakyThrows @Around("onCreatePointcut()") public Object addActivity(final ProceedingJoinPoint joinPoint) { Object object = joinPoint.getTarget(); if (!(object instanceof Activity)) { return joinPoint.proceed(); } Activity activity = (Activity) object; Application application = activity.getApplication(); MyActivityLifecycleCallbacks myActivityLifecycleCallbacks = new MyActivityLifecycleCallbacks(); application.registerActivityLifecycleCallbacks(myActivityLifecycleCallbacks); Log.i(activity.getClass().getName(), "aop execute correct"); return joinPoint.proceed(); } }
|