package com.vortex.test;
import android.view.View;
/**
* Created by Administrator (chenPS) on 2019/10/8.
*/
public class HookListener implements View.OnClickListener{
private long lastClick=0;
private View.OnClickListener mOnClickListener;
public HookListener(View.OnClickListener onClickListener) {
mOnClickListener = onClickListener;
}
@Override
public void onClick(View v) {
long nowClick=System.currentTimeMillis();
if(nowClick-lastClick>2000){
mOnClickListener.onClick(v);
}
}
}
package com.vortex.test;
import android.view.View;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by Administrator (chenPS) on 2019/10/8.
*/
public class HookView {
public static void hookView(View view){
try {
/**
* 标准类的 mlistener 属性
*/
Class<?> lisInfo=Class.forName("android.view.View$ListenerInfo");
Field listener=lisInfo.getDeclaredField("mOnClickListener");
listener.setAccessible(true);
/**
* 反射出来的 view 属性
*/
Method vLisInfo=View.class.getDeclaredMethod("getListenerInfo");
vLisInfo.setAccessible(true);
Object viewListenerInfo=vLisInfo.invoke(view);
/**
* 获取反射view 的 属性
*/
View.OnClickListener originClicklistener= (View.OnClickListener) listener.get(viewListenerInfo);
View.OnClickListener hookListener=new HookListener(originClicklistener);
listener.set(viewListenerInfo,hookListener);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
出处:https://blog.csdn.net/qq_32532321/article/details/80476797
网友评论