This article describes the Android double click Return key to exit the program, is a very useful Android program development function, share for everyone to use for reference. The specific methods are as follows:
First, the realization of ideas:
When the user presses the return key to set a timer to monitor whether 2 seconds to achieve the exit, if the user does not press the return key, then clear the first press RETURN key effect, so that the program restore the first time the return key before the state. The timer is created every time the user presses the return key.
Second, the function code:
/**
* menu, Return key response
* *
@Override public
boolean onKeyDown (int keycode, keyevent event) {
//TODO auto-generated method Stub
if (keycode = = Keyevent.keycode_back)
{
exitby2click ();//call double repel out function
} return
false;
}
/**
* Double repel out function
*
/private static Boolean Isexit = false;
private void Exitby2click () {
Timer texit = null;
if (Isexit = = False) {
Isexit = true;//ready to exit
Toast.maketext (This, "Press Exit Again", Toast.length_short). Show ();
texit = new Timer ();
Texit.schedule (New TimerTask () {
@Override public
void Run () {
isexit = false;//Cancel Exit
}
}, 2000); If the return key is not pressed within 2 seconds, the start timer cancels the task that was just performed
} else {
finish ();
System.exit (0);
}
}
The OnKeyDown () function in the above code can be found in the menu bar source->override/implement methods, which is automatically added to your code .
I hope this article will help you with your Android program.