Skip to content

Instantly share code, notes, and snippets.

@alitamoor65
Created April 10, 2018 03:14
Show Gist options
  • Save alitamoor65/a32805d1c4668e13585d5bc9d46ff04d to your computer and use it in GitHub Desktop.
Save alitamoor65/a32805d1c4668e13585d5bc9d46ff04d to your computer and use it in GitHub Desktop.
Press Again To Exit With Timer
private long lastPressedTime;
private static final int PERIOD = 2000;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
if (event.getDownTime() - lastPressedTime < PERIOD) {
finish();
} else {
Toast.makeText(getApplicationContext(), "Press again to exit.",
Toast.LENGTH_SHORT).show();
lastPressedTime = event.getEventTime();
}
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment