Android basic tutorial _ press the two return key to exit the program and long press the return key to exit the program

Source: Internet
Author: User

The above two functions mainly refer to the blog:

Http://blog.csdn.net/chenshijun0101/article/details/7682210

Http://blog.csdn.net/winlinking/article/details/6596455

Method 1:

Press the return key twice to exit the program:

Public class exit {

private boolean isExit = false;private Runnable task = new Runnable() {@Overridepublic void run() {isExit = false;}};public void doExitInOneSecond() {isExit = true;HandlerThread thread = new HandlerThread("doTask");thread.start();new Handler(thread.getLooper()).postDelayed(task, 1000);}public boolean isExit() {return isExit;}public void setExit(boolean isExit) {this.isExit = isExit;}}

Activity:

Public class back2finishappactivity extends activity {exit = new exit ();/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main) ;}@ override public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) {pressagainexit (); Return true;} return Super. onkeydown (keycode, event);} private void pressagainexit () {If (exit. isexit () {finish ();} else {toast. maketext (getapplicationcontext (), "exit the program again", toast. length_short ). show (); exit. doexitinonesecond ();}}}

Long press the return key to exit the program:

public boolean dispatchKeyEvent(KeyEvent event)      {          int keyCode=event.getKeyCode();          switch(keyCode)          {              case KeyEvent.KEYCODE_BACK: {                   if(event.isLongPress())                   {                       this.stopService(intent);                       System.exit(0);                       return true;                   }else                   {                       return false;                       }              }            }          return super.dispatchKeyEvent(event);                }

Method 2:

  1. Private long exittime = 0;
  2.  
  3. @ Override
  4. Public Boolean onkeydown (INT keycode, keyevent event ){
  5. If (keycode = keyevent. keycode_back & event. getaction () = keyevent. action_down ){
  6. If (system. currenttimemillis ()-exittime)> 2000 ){
  7. Toast. maketext (getapplicationcontext (), "exit the program once again", Toast. length_short). Show ();
  8. Exittime = system. currenttimemillis ();
  9. } Else {
  10. Finish ();
  11. System. Exit (0 );
  12. }
  13. Return true;
  14. }
  15. Return super. onkeydown (keycode, event );
  16. }

Welcome reprinted: http://blog.csdn.net/johnny901114/article/details/7823048

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.