Android 连续点击启动隐藏功能

sancaiodm Android应用 2022-07-21 1288 0

实现方式1

    private long[] mHits = new long[5];
    private void ShowServerSetting() {
        System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);//移动数据
        mHits[mHits.length-1] = SystemClock.uptimeMillis();//新点击时获取的当前时间存到数组的最后一个
        if (mHits[0] >= (mHits[mHits.length-1] - 20000)) {//第一次和最后一次点击的时间间隔小于20000ms(20s),判定为有效的5次点击
            Intent intent = new Intent(LoginActivity.this, AppServerActivity.class);
            startActivity(intent);
        }
    }

实现方式2

 
androidos.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        long currentClickTime = SystemClock.uptimeMillis();
        long elapsedTime = currentClickTime - mLastClickTime;
        mLastClickTime = currentClickTime;
        if (elapsedTime < intervalTime1000) {
            ++monClickNum;
            if (7 == monClickNum) {
                //do somethings
            }
        } else {
            monClickNum = 0;
        }

    }
});

实现方式3 该方式就与方式2无差异,一个递增计数,一个递减计数

.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (breakthrough > 0) {
                    if (isFastClickEachOneSec()) {
                        --breakthrough;
                        if (breakthrough == 0) {
                            //do somethings
                            return;
                        }
                        if (breakthrough < BREAK_THROUGH_TIMES - 1)
                            Toask.make( "点击" + breakthrough + "次开放手动权限");
                    } else
                        breakthrough = BREAK_THROUGH_TIMES;
                }
            }
        });
        

Integer BREAK_THROUGH_TIMES = 7;
int breakthrough = BREAK_THROUGH_TIMES;
long lastClickTime;

public boolean isFastClickEachOneSec() {
	long time = System.currentTimeMillis();
	if (time - lastClickTime < 1000) {
		lastClickTime = time;
		return true;
	}
	astClickTime = time;
	return false;
}


评论