Reputation: 389
I'm trying to do some hidden menu implementation. Something like Developers options in Android OS where you press 10 times and it opens a secret menu.
MY task is to make a onbutton click listener that fires some kind of timer event. I have to click that button 10 times and the secret menu will appear. What is more it should have 8 second reset timer that resets the clicks if the user didnt press the button in 8 seconds. I understand it should be done on worker thread but I have a problem finding best practices and classes for this kind of work.
Upvotes: 0
Views: 1809
Reputation: 3725
You need not to actually implement timer here, just keep a note of time when user hits the button and compare the difference everytime if it is under 8 seconds, reset otherwise.
Assume you have some method like manageAction()
long lastHitTime;
int counter;
boolean manageAction() {
if (System.currentTimeMillis() - lastHitTime <= 8000) {
counter++;
return counter>=10;
}
counter=0;
return false;
}
Upvotes: 2
Reputation: 389
This is my solution I managed with your help guys.
private long startTime;
private boolean isClicked = false;
private short clickCount;
tvPhoneTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isClicked) {
startTime = System.currentTimeMillis();
isClicked = true;
clickCount++;
}
if (System.currentTimeMillis() - startTime <= 10000) {
clickCount++;
if (clickCount == 8) {
//DO SOMETHING
clickCount = 0;
}
} else {
clickCount = 0;
isClicked = false;
}
}
});
Upvotes: 0
Reputation: 3754
Try this
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (clicks == 0) {
clickCountStartTime = Calendar.getInstance().getTimeInMillis();
}
long endTime = clickCountStartTime + 3000;
if (Calendar.getInstance().getTimeInMillis() > endTime) {
clicks = 0;
}
if (clicks == 10) {
//Code of your intent, toast or whatever action goes here
clicks = 0;
}
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
startClickTime = Calendar.getInstance().getTimeInMillis();
break;
}
case MotionEvent.ACTION_UP: {
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration < MAX_CLICK_DURATION) {
clicks++;
}
}
}
return super.dispatchTouchEvent(ev);
}
Upvotes: 0
Reputation: 1216
I've done something like your task, so just try this.
first declare two parameters, one counts your hit times, the other indicates whether hit times should be reset.
int hitTimes = 0;
boolean isTheFirstHit = true;
then, add listener and create the method to count hit times.
YOUR_BUTTON.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSecretMenu();
countTimes();
}
});
private void countTimes(){
if (isTheFirstHit){
Log.d("YOUR TAG", "hitTimes = " + hitTimes);
isTheFirstHit = false;
new Thread(){
@Override
public void run() {
long start = System.currentTimeMillis();
Log.d("YOUR TAG", "start time: " + start);
while (true){
SystemClock.sleep(500);
long end = System.currentTimeMillis();
if (end - start > 8000 ){
isTheFirstHit = true;
clickTimes = 0;
Log.d("YOUR TAG", "reset hitTimes after 8 seconds");
break;
}
}
}
}.start();
}
}
then, show your secret menu when you constantly hit the button 8 times
private void showSecretMenu(){
hitTimes++;
Log.d("YOUR TAG", "current hit times = " + hitTimes);
if (hitTimes == 8 && !isTheFirstHit){
// SHOW YOUR SECRET MENU HERE
}
}
Upvotes: 0
Reputation: 1091
You can try this,
public class MainActivity extends AppCompatActivity {
private int clickCount = 0;
private int maxClick = 10;
private Toast mToast;
private long clickDelayTime = 500; // milli second (if you want to set 8 sec then set 8000 instead of 500)
private CountDownTimer mCountDownTimer = new CountDownTimer(clickDelayTime, clickDelayTime) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
// set click count to 0 if user stop clicking before timer finish
clickCount = 0;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tvClick = (TextView) findViewById(R.id.tvClick);
tvClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// increment click count
clickCount++;
// cancel previous timer
mCountDownTimer.cancel();
// start new timer after next click
mCountDownTimer.start();
if (clickCount == maxClick) {
// cancel current toast
mToast.cancel();
Toast.makeText(MainActivity.this, "You have clicked " + clickCount + " times.", Toast.LENGTH_SHORT).show();
clickCount = 0;
return;
}
// create toast is null
if (mToast == null) {
mToast = Toast.makeText(MainActivity.this, "Click " + (maxClick - clickCount) + " more times.", Toast.LENGTH_SHORT);
} else {
// if toast is not null then update message
mToast.setText("Click " + (maxClick - clickCount) + " more times.");
}
mToast.show();
}
});
}
}
Upvotes: 2