Android implementation clicks two times back key to exit application _android

Source: Internet
Author: User
Tags time interval

Idea: Android Capture user button is in the OnKeyDown method, just to determine whether the user button is keycode_back that is the back button, the rest is to judge the two times click the time interval of the button.

The first implementation method:

  Package com.example.clickexittest; 
  Import android.app.Activity; 
  Import Android.os.Bundle; 
  Import Android.os.Handler; 
  Import Android.os.Message; 
  Import Android.util.Log; 
  Import android.view.KeyEvent; 
   
  Import Android.widget.Toast; public class Mainactivity extends activity {private static final String TAG = MainActivity_Exit.class.getSimpl 
   
    Ename (); 
   
    Define a variable to identify whether to exit the private static Boolean isexit = false; 
        private static Handler Mhandler = new Handler () {@Override public void Handlemessage (msg) { 
        Super.handlemessage (msg); 
      Isexit = false; 
     
    } 
    }; 
      @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
       
    Setcontentview (R.layout.activity_main); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode _back) {exit ();
        return true; 
    Return Super.onkeydown (KeyCode, event); 
        private void Exit () {if (!isexit) {isexit = true; 
        Toast.maketext (Getapplicationcontext (), "Press the back key again to exit the program", Toast.length_short). Show (); 
      Use handler delay to send Change status information mhandler.sendemptymessagedelayed (0, 2000); 
         
        else {LOG.E (TAG, "Exit Application"); 
      This.finish (); 

 } 
    } 
  }

In the Exit method, the value of the isexit is first judged, and if False, true, the prompt is popped, and a message is issued after 2000 milliseconds (2 seconds) to restore the value to false in handler. If the back key is pressed again within 2 seconds of the message interval being sent, the exit method is executed again, when the Isexit value is already true, and the exit method is executed.

The second way to implement it:

  Package com.example.clickexittest; 
  Import android.app.Activity; 
  Import Android.os.Bundle; 
  Import Android.util.Log; 
  Import android.view.KeyEvent; 
   
  Import Android.widget.Toast; public class Mainactivity extends activity {private static final String TAG = MainActivity.class.getSimpleName () 
   
    ; Private long clicktime = 0; Record the time of the first click @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinsta 
      Ncestate); 
   
    Setcontentview (R.layout.activity_main); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_b 
        ACK) {exit (); 
      return true; 
    Return Super.onkeydown (KeyCode, event); private void Exit () {if (System.currenttimemillis ()-Clicktime) >) {toast.maketext ( 
        Getapplicationcontext (), "Press the back key again to exit the program", Toast.length_short). Show (); ClicktIME = System.currenttimemillis (); 
        else {LOG.E (TAG, "Exit Application"); 
  This.finish (); 
      System.exit (0); 
 } 
    } 
  }

Judge the user two times whether the time difference is within an expected value, is a direct exit directly, not the words prompt the user to press the back key again exit.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.