Android Continuous Click two times back key exit program

Source: Internet
Author: User

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

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.