Android returns two buttons to exit the program, and android keys to exit the program.

Source: Internet
Author: User

Android returns two buttons to exit the program, and android keys to exit the program.


Public boolean onKeyDown (int keyCode, KeyEvent event) {// two return exit program if (keyCode = KeyEvent. KEYCODE_BACK) {if (System. currentTimeMillis ()-mExitTime)> 2000) {Toast. makeText (this, R. string. toast_twice_press_back_to_exit, Toast. LENGTH_SHORT ). show (); mExitTime = System. currentTimeMillis () ;}else {finish () ;}return true ;}return super. onKeyDown (keyCode, event );}

The function is placed in your own Activity. Remember to define the mExitTime variable and initialization. It has been verified and the effect is good!


How to add a connection in android and press the exit key twice to exit the program

The principle is to calculate the time interval between two presses. You can set the value by yourself. If the interval is smaller than the specified value, it is considered to be two consecutive presses. At this time, exit
Private long mExitTime;
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
If (System. currentTimeMillis ()-mExitTime)> 800 ){
Toast. makeText (this, "exit the program again", Toast. LENGTH_SHORT). show ();
Mexico time = System. currentTimeMillis ();

} Else {
Finish ();
}
Return true;
}
Return super. onKeyDown (keyCode, event );
}
 
In the Android program, press the return key to exit the program. I want to press the return key to return to the previous interface. How should I implement this?

If you use setcontentview, that is, the activity is not actually switched, you have to handle the event that returns the key by yourself. When you click return, setcontentview is the one on the previous page.

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.