فهرست منبع

动态权限申请、快速点击

詹子聪 5 سال پیش
والد
کامیت
f8cbcdba56

+ 6 - 0
app/build.gradle

@@ -1,4 +1,5 @@
 apply plugin: 'com.android.application'
+apply plugin: 'kotlin-android'
 apply plugin: 'com.hujiang.android-aspectjx'
 
 
@@ -61,8 +62,13 @@ dependencies {
     compile 'org.aspectj:aspectjrt:1.8.+'
     // 动态权限申请firefly1126/android_permission_aspectjx
     compile 'com.firefly1126.permissionaspect:permissionaspect:1.0.1'
+    implementation "androidx.core:core-ktx:+"
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
 }
 
 aspectjx {
     exclude  "android.support",'androidx','com.google','com.squareup.leakcanary','com.squareup.leakcanary.core','com.alipay','org.apache','com.tencent'
+}
+repositories {
+    mavenCentral()
 }

+ 27 - 0
app/src/main/java/com/miekir/newmvp/aspectj/click/SingleClick.kt

@@ -0,0 +1,27 @@
+package com.miekir.newmvp.aspectj.click
+
+/**
+ * AnnotationRetention.SOURCE:不存储在编译后的 Class 文件。
+ * AnnotationRetention.BINARY:存储在编译后的 Class 文件,但是反射不可见。
+ * AnnotationRetention.RUNTIME:存储在编译后的 Class 文件,反射可见。
+ */
+@Retention(AnnotationRetention.RUNTIME)
+/**
+ * AnnotationTarget.CLASS:类,接口或对象,注解类也包括在内。
+ * AnnotationTarget.ANNOTATION_CLASS:只有注解类。
+ * AnnotationTarget.TYPE_PARAMETER:Generic type parameter (unsupported yet)通用类型参数(还不支持)。
+ * AnnotationTarget.PROPERTY:属性。
+ * AnnotationTarget.FIELD:字段,包括属性的支持字段。
+ * AnnotationTarget.LOCAL_VARIABLE:局部变量。
+ * AnnotationTarget.VALUE_PARAMETER:函数或构造函数的值参数。
+ * AnnotationTarget.CONSTRUCTOR:仅构造函数(主函数或者第二函数)。
+ * AnnotationTarget.FUNCTION:方法(不包括构造函数)。
+ * AnnotationTarget.PROPERTY_GETTER:只有属性的 getter。
+ * AnnotationTarget.PROPERTY_SETTER:只有属性的 setter。
+ * AnnotationTarget.TYPE:类型使用。
+ * AnnotationTarget.EXPRESSION:任何表达式。
+ * AnnotationTarget.FILE:文件。
+ * AnnotationTarget.TYPEALIAS:@SinceKotlin("1.1") 类型别名,Kotlin1.1已可用。
+ */
+@Target(AnnotationTarget.FUNCTION)
+annotation class SingleClick

+ 45 - 0
app/src/main/java/com/miekir/newmvp/aspectj/click/SingleClickAspect.kt

@@ -0,0 +1,45 @@
+package com.miekir.newmvp.aspectj.click
+
+import org.aspectj.lang.ProceedingJoinPoint
+import org.aspectj.lang.annotation.Around
+import org.aspectj.lang.annotation.Aspect
+import org.aspectj.lang.annotation.Pointcut
+import java.util.*
+
+//使用@Aspect注解标示这是一个切面类
+@Aspect
+class SingleClickAspect {
+     //@Pointcut来标识所要寻找的切点,就是我们定义的@ SingleClick注解
+    @Pointcut("execution(@com.miekir.newmvp.aspectj.click.SingleClick * *(..))")//方法切入点
+    fun methodAnnotated() {
+
+    }
+    
+     /**
+     * joinPoint.proceed() 执行注解所标识的代码
+     * @After 可以在方法前插入代码
+     * @Before 可以在方法后插入代码
+     * @Around 可以在方法前后各插入代码
+     */
+    @Around("methodAnnotated()")
+    //@Throws这个注解不必在意,这个是kotlin的注解,标识该方法可以抛出异常
+    @Throws(Throwable::class)
+    fun aroundJoinPoint(joinPoint: ProceedingJoinPoint) {
+        //获取系统当前时间
+        val currentTime = Calendar.getInstance().timeInMillis
+        //当前时间-上次记录时间>过滤的时间 过滤掉600毫秒内的连续点击
+        //表示该方法可以执行
+        if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
+            //将刚进入方法的时间赋值给上次点击时间
+            lastClickTime = currentTime
+            //执行原方法
+            joinPoint.proceed()
+        }
+    }
+
+    companion object {
+        const val TAG = "SingleClickAspect"
+        const val MIN_CLICK_DELAY_TIME = 600
+        var lastClickTime = 0L
+    }
+}

+ 2 - 0
build.gradle

@@ -1,6 +1,7 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 
 buildscript {
+    ext.kotlin_version = '1.4.20'
     repositories {
         google()
         jcenter()
@@ -9,6 +10,7 @@ buildscript {
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.1'
         classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
     }
 }