idea 1: Record the last click Time, compared with the time of the click, when two time interval is less than a certain value, exit, otherwise prompt "Press one more time to exit the program , while updating the last click Time
private long firsttime = 0; @ Override public boolean onkeyup (int keycode, keyevent event) { // TODO Auto-generated method stub switch (keycode) { case KeyEvent.KEYCODE_BACK: long secondtime = system.currenttimemillis (); if (secondtime - firsttime > 2000) { //if the two-time key time interval is greater than 2 seconds, do not exit toast.maketext (this, "Press again to exit the program", toast.length_short). Show (); firsttime = secondtime;//Update firsttime return true; } else { //Two-time keys less than 2 seconds, exit the app system.exit (0); } break; } return super.onkeyup (keycode, event); }
idea 2: Time delay processing
private int mbackkeypressedtimes = 0; @Override public void onbackpressed () { if (mbackkeypressedtimes == 0) { toast.maketext (this, "Press and Exit program ", toast.length_short). Show (); mBackKeyPressedTimes = 1; new thread () &NBsp { @Override public Void run () { try { thread.sleep (; ) } catch ( Interruptedexception e) { e.printstacktrace (); } finally { mBackKeyPressedTimes = 0; } } }.start (); return; else{ this.activity.finish (); } } super.onbackpressed (); }
Android Continuous Click two times back key exit program