Android implementation Click two times back key to exit

Source: Internet
Author: User

In the Android application is we often want to judge the user to return key operation, generally in order to prevent misoperation is the user repeatedly press two times the return key when the user is prompted to quit the application.

Principle:

Listen to the back key press, you need to rewrite the OnKeyDown () method, determine the two times the time difference is pressed, if less than 2 seconds, exit, and vice versa.


The first type:

Package Com.example.appexitdemo;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.view.keyevent;import Android.widget.toast;public class Mainactivity extends Actionbaractivity {  @ override protected void OnCreate (Bundle savedinstancestate) {  super.oncreate (savedinstancestate);  Setcontentview (R.layout.activity_main);  } private long exittime = 0;     @Override   public boolean onKeyDown (int keycode, keyevent event) {  if (keycode = = keyevent.keycode_back) {   exitbyti Me ();   return false; }  return Super.onkeydown (KeyCode, event);  } public void Exitbytime () {  if ((System.currenttimemillis ()-exittime) >) {   toast.maketext ( Getapplicationcontext (), "Press again to exit the program",     toast.length_short). Show ();   exittime = System.currenttimemillis (); } else {   finish ();   system.exit (0); } }  }
The second type:
Package Com.example.appexitdemo;import Android.os.bundle;import Android.os.handler;import android.os.Message; Import Android.support.v7.app.actionbaractivity;import Android.view.keyevent;import Android.widget.Toast;public Class Mainactivity extends actionbaractivity {   @Override  protected void OnCreate (Bundle savedinstancestate) {  super.oncreate (savedinstancestate);  Setcontentview (R.layout.activity_main);  } //defines a variable to identify whether to exit     private static Boolean isexit = false;        Handler Mhan Dler = new Handler () {        @Override         public void Handlemessage (Message m SG) {            super.handlemessage (msg);            Isexit = false;       }   };      @Override  public boolean onKeyDown (int keycod E, keyevent event) {  if (keycode = = keyevent.keycode_back) {    exit ();   return false; }  return Super.onkeydown (KeyCode, event);  }  private void exit () {        if (!isexit) {            Isexit = true;  &nbs P         Toast.maketext (Getapplicationcontext (), "Press again to exit the program",                    toast.length_short). Show ();           //Use handler delay to send Change status information &N Bsp           mhandler.sendemptymessagedelayed (0, +);       } else {  &N Bsp         finish ();            system.exit (0);        }   }}

Android implementation Click two times back key to exit

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.