Created
April 10, 2018 03:14
-
-
Save alitamoor65/a32805d1c4668e13585d5bc9d46ff04d to your computer and use it in GitHub Desktop.
Press Again To Exit With Timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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