Android Double click Back key to Exit program implementation

Source: Internet
Author: User

in the development of the application, there is a feature is very common, that is, quickly double-click the Return button, and then realize the function of exiting activity. I read a lot of information on the Internet code, summed up, there are two main ways to better. One is the time delay execution of the thread, and the other is the realization of recording the key times. Now share with you, the code is as follows, I hope to help you:

 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  }                          & 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 ();
}

Third

Private LongLastpressedtime;Private Static Final IntPERIOD= 2000;@OverridePublic BooleanOnKeyDown(IntKeyCode, KeyEventEvent) { 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.show ();  Lastpressedtime =  Event.} return true  } } return  false;               /span>                

Android Double click Back key to Exit program implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.