Android two times press the back button to exit the client

Source: Internet
Author: User

Android in some apps pop-up dialog when exiting the client, there are two clicks, the first is to prompt the user whether to quit the client, the second click is the real exit app, which is two times the click Back key time interval to judge,

Realize this simple function today,


Analysis: The program to save the user's first click on the time, in Java we know, save a variable is definitely to define a member variable to save the first click of the time variable, when the second click to get the current click Time, and the first click on the time to subtract whether more than how many seconds to let the app exit, code implementation

Public  long  pretime;    public static final Long Two_second = 2 * 1000; @Override public    void onbackpressed () {            Long currenttime = System.currenttimemillis ();            If the time interval is greater than 2 seconds, do not process            if ((currenttime-pretime) > Two_second) {                //Display message                Toast.maketext (this, "Press again to exit the client", 0 ). Show ();                Update time                pretime = currenttime;                return;                Intercept the event and no longer process            }else{            finish ();}    }


Android two times press the back button to exit the client

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.