androiddouble-clickheadset

How to handle double click from headset hook?


I would like to control my squash score counter app via only one head set button. It means that I want to detect single or double click and add score to first or second player according to number of clicks.

I cannot use long click, instead of double click, because long click activate Google Now.


Solution

  • This is what I used in my music player to handle headset control single and double click.

    static final long CLICK_DELAY = 500;
    static long lastClick = 0; // oldValue
    static long currentClick = System.currentTimeMillis();
    
    @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
    
                KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
    
                if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)return;
    
                lastClick = currentClick ;
    
                currentClick = System.currentTimeMillis();
    
                if(currentClick - lastClick < CLICK_DELAY ){
    
                     //This is double click
    
                } else { 
    
                     //This is single click
    
                }
            }