Android Double-click the return key to exit the program implementation method _android

Source: Internet
Author: User

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.

Related Article

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.