The implementation and analysis of the exit program by double-clicking prompt

Source: Internet
Author: User

At the end of last year when I was learning Android to write a demo, again want to learn to write a double exit logic, but at that time to see good dozens of lines of code and logic on the spot, but recently in the project, suddenly think of this logic, think of looking for a look, And then found it is quite simple ~ ~ ~ ~ ~ ~ ~ ~


It's easy to think of a timer here that will open up an asynchronous timing task for us:

   @Override public    void onbackpressed () {        exitby2click ();    }    private void Exitby2click () {        timer texit= null;//timer definition, it is recommended that the variable be written to the active member variable, avoiding the waste of stack memory, where empty can achieve the effect of releasing heap memory, preventing Oom        if (!isexit) {//is false, the initialization timer starts timing, true indicates that the delay task was not executed, and entered an else            isexit= true;//Start Timing identification            toast.maketext (this, "Press again to exit the program" , Toast.length_short). Show ();            texit= new Timer ();            Texit.schedule (New TimerTask () {//Schedule a delay task                @Override public                void Run () {//The task is executed, Indicates that the Onbackpressed method is not triggered again within two seconds, and the isexit tag is reset to False. This time the countdown end of the 2s                    isexit= false;                }            },2000);//two seconds after executing the code block in run, to end this second combo monitoring (asynchronous execution of the timing task, Can also be understood as a sleep on a child thread)        }else {            finish ();            System.exit (0);//Because there is no activity in the activity stack, you may not use this mandatory exit code for the time being, but it is recommended to use        }    }

The Exitby2click is our core code ~ ~ ~ In fact, the core is a delay task + tag amount

The specific logic is explained in detail in the comments, the same for the return key monitoring we can also use onkeydown to monitor the specific key events ...

  @Override//Listen back key public    boolean onKeyDown (int keycode, keyevent event) {        if (keycode== keyevent.keycode_back) {            Exitby2click ();        }        return false;    }


Of course, the implementation of the core logic can also be implemented using handler postdelayed, where the role of handler object is equivalent to the Timer object, so I only post a simple demo, left by the Bo friends of their own brain repair it

<span style= "FONT-SIZE:14PX;" >new Handler (). postdelayed (New Runnable () {            @Override public            void Run () {            }        },2000);</span>


The implementation and analysis of the exit program by double-clicking prompt

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.