0

I'm trying to implement a long key press recognition, I've overridden the onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
//          event.startTracking();
        if (backPressedStarted == -1) {
            progressBar.setProgress(0);
            progressBar.setVisibility(View.VISIBLE);
            backPressedStarted = System.currentTimeMillis();        
        } else {
            int pressDuration = (int) (System.currentTimeMillis() - backPressedStarted);
            progressBar.setProgress(pressDuration);
            if (pressDuration > 5000)
                showExitDialog();
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

After ~3.8 sec the system dispatches an onKeyUp event with the flag: FLAG_CANCELED

I've tried ignoring it and return true or false in the key up method but it did not seem to work...

Is there a way around this? could I define the longest press duration?

== UPDATE ==

Ok, so when I remove the comment for the event tracking, the long click call back is called, and then returning true or false still does not change the end result of keyUp cancels after that period of time.

Thanks.

TacB0sS
  • 10,106
  • 12
  • 75
  • 118

1 Answers1

0

After digging and trying multiple approaches attempting to override this, (with the exception of injecting my own custom touch event, i didn't feel like managing this hack), I've found out that this is not possible to avoid this "feature".

My solution was to shortened the time from 5000ms, to 3000ms, and allowed another exit pattern a side of this one, for devices with OS that might surprise me with a cancel event before 3000ms...

TacB0sS
  • 10,106
  • 12
  • 75
  • 118