How to handle double-click from the headset?

I would like to control the squash counter application with just one head set button. This means that I want to detect a single or double click and add an account to the first or second player depending on the number of clicks.

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

+4
source share
1 answer

This is what I used in my music player to control one and two clicks of the mouse.

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

            }
        }
0
source

All Articles