First, define a variable of the current time: private long mexittime; This constant is used to calculate the current time. The code is as follows:
Mexittime = System.currenttimemillis ();//update mexittime to current time
Defines the maximum time interval for the two-click Exit button: Private long waittime=2000; When the time interval is less than waitTime exit the program, greater than WaitTime when the click is the first click, will pop up again click to exit the prompt.
The onkeydown event of the page is then implemented. The code is as follows:
public boolean onKeyDown (int keycode, keyevent event) {
TODO auto-generated Method Stub
if (keycode==keyevent.keycode_back) {
if ((System.currenttimemillis ()-mexittime) >waittime) {
Toast.maketext (This, "Press the Eject program again", Toast.length_short). Show ();
Mexittime=system.currenttimemillis ();
}
else {
System.exit (0);
}
return true;
}
Return Super.onkeydown (KeyCode, event);
}
- The principle of its implementation is to take you two times when you click the Exit button. Two times the interval is less than 2000 milliseconds to realize the exit, when two times the time interval size 2000, give the user a press again to exit the hint. What do you think. It is not very convenient to do so. Of course, there is a place to pay attention to, that is, you want to implement this function in which activity to put this code, if you want to put too many pages, then you write a class, in the activity to inherit the class.
"Press again to exit" feature in Android