androidtouch-eventkiosk

Tap on screen 5 Times in 3 seconds in android


I am developing a kiosk and now in admin side. In order to go to the Admin, the user needs to tap the screen 5 times just in 3 seconds or else, nothing will happen.


Solution

  • Please read the comments in the code, it is quite straightforward

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
     
    public class MainActivity extends Activity {
       
    private int tapCount = 0;
    private long tapCounterStartMillis = 0;
    
    //detect any touch event in the screen (instead of an specific view)
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {  
        
        int eventaction = event.getAction();
         if (eventaction == MotionEvent.ACTION_UP) {
         
         //get system current milliseconds
         long time= System.currentTimeMillis();
    
    
         //if it is the first time, or if it has been more than 3 seconds since the first tap ( so it is like a new try), we reset everything 
         if (tapCounterStartMillis == 0 || (time-tapCounterStartMillis > 3000) ) {
             tapCounterStartMillis = time;
             tapCount = 1;
         }
         //it is not the first, and it has been  less than 3 seconds since the first
         else{ //  time-tapCounterStartMillis < 3000   
             tapCount ++;
         }
         
         if (tapCount == 5) {
            //do whatever you need
         }
         return true;    
        }
        return false;
      }