The first: the use of thread-delay implementation:
private int mbackkeypressedtimes = 0;
@Override public void onbackpressed () {if (Mbackkeypressedtimes = = 0) {Toas T.maketext (This, "Press once again to exit the program ", Toast.length_short). Show (); mbackkeypressedtimes = 1; & nbsp new Thread () { & nbsp; @Override public void Run ( ) { Try { &nbs p; thread.sleep (&NB);Sp & nbsp; } catch (Interruptedexception e) {  &NB Sp & nbsp; e.printstacktrace (); & nbsp } finally { & nbsp mbackkeypressedtimes = 0; & nbsp; & nbsp;   } & nbsp; } and nbsp; }.start (); return; else{ & nbsp; This.activity.finish (); } } super.onbackpressed (); }
Second: The use of the Computation time difference implementation (personally think this way is relatively simple, and not prone to anomalies, the code is more secure)
Private long exittime = 0;
public void Exitapp ()
{
if ((System.currenttimemillis ()-Exittime) > 2000)
{
Toast.maketext (This.activity, "Press again to exit the program", Toast.length_short). Show ();
Exittime = System.currenttimemillis ();
} else
{
This.activity.finish ();
}
}
The third method of
- /**
- * menu, Return key response
- */
- @Override
- Public boolean onKeyDown (int keycode, keyevent event) {
- //TODO auto-generated method stub
- if (keycode = = Keyevent.keycode_back)
- {
- Exitby2click (); //Call the double Exit function
- }
- return false;
- }
- /**
- * Double Exit function
- */
- Private static Boolean Isexit = false;
- Private void Exitby2click () {
- Timer texit = null;
- if (Isexit = = false) {
- Isexit = true; //Ready to exit
- Toast.maketext (This, "Press again to exit the program", 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 out the task just performed
- } Else {
- Finish ();
- System.exit (0);
- }
- }
Android Double-click the return button to exit Activity method