CheckDoubleUtil
/**
* @author ZhangChengBo
* Created by Android Studio.
* @description:
* @date :2019/12/5 11:37
*/
public class CheckDoubleUtil {
private StringmActivityJumpTag; //activity跳转tag
private long mClickTime; //activity跳转时间
private volatile static CheckDoubleUtilinstance;
//双重锁验证单例模式
private CheckDoubleUtil(){
}
public static CheckDoubleUtilgetInstance(){
if (instance ==null){
synchronized (CheckDoubleUtil.class){
if (instance ==null){
instance =new CheckDoubleUtil();
}
}
}
return instance;
}
/**
* 检查是否重复跳转,不需要则重写方法并返回true
*/
public boolean checkDoubleClick(Intent intent) {
// 默认检查通过
boolean result =true;
// 标记对象
String tag;
if (intent.getComponent() !=null) {// 显式跳转
tag = intent.getComponent().getClassName();
}else if (intent.getAction() !=null) {// 隐式跳转
tag = intent.getAction();
}else {
return true;
}
if (tag.equals(mActivityJumpTag) &&mClickTime >= SystemClock.uptimeMillis() -500) {
// 检查不通过
result =false;
}
// 记录启动标记和时间
mActivityJumpTag = tag;
mClickTime = SystemClock.uptimeMillis();
return result;
}
使用方法(Activity中添加):
/**
* 每次启动activity都会调用此方法
*/
@SuppressLint("RestrictedApi")
@Override
public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
if (CheckDoubleUtil.getInstance().checkDoubleClick(intent)) {
super.startActivityForResult(intent, requestCode, options);
}
}