直接借鉴开发者模式怎么实现的就可以了。如下。
先声明成员变量:
final static int COUNTS = 10;//点击次数
final static long DURATION = 5 * 1000;//规定有效时间
long[] mHits = new long[COUNTS];
设置点击事件:
app_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);//每次点击时,数组向前移动一位[1]
mHits[mHits.length - 1] = SystemClock.uptimeMillis();//为数组最后一位赋值
if ((SystemClock.uptimeMillis() - mHits[0]) <= DURATION) {//检查是否是在我们规定的时间内
mHits = new long[COUNTS];//重新初始化数组
//执行对应的逻辑
try {
Intent intent = new Intent();
intent.setClass(context,HiddenActivity.class);
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
以上代码的意思是:
触发点击的时候都将数组向左移动一位,将时间赋值给最后一位,从上面的代码中可以看出当点击了 10次, 那么最后一位就已经被移到了第一位,然后比较时间:mHits[0] >= (SystemClock.uptimeMillis() - DURATION) 如果是在规定的时间内触发的连续点击,那么就生效,执行所要的操作。
[1] If the <code>src</code> and <code>dest</code> arguments refer to the same array object, then the copying is performed as if the components at positions <code>srcPos</code> through <code>srcPos+length-1</code> were first copied to a temporary array with <code>length</code> components and then the contents of the temporary array were copied into positions <code>destPos</code> through <code>destPos+length-1</code> of the destination array. Explanation from the official Android of the System.arraycopy() method.