"Press again to exit" feature in Android

Source: Internet
Author: User

  1. First, define a variable of the current time: private long mexittime; This constant is used to calculate the current time. The code is as follows:

    Mexittime = System.currenttimemillis ();//update mexittime to current time

    Defines the maximum time interval for the two-click Exit button: Private long waittime=2000; When the time interval is less than waitTime exit the program, greater than WaitTime when the click is the first click, will pop up again click to exit the prompt.

  2. The onkeydown event of the page is then implemented. The code is as follows:

    public boolean onKeyDown (int keycode, keyevent event) {
    TODO auto-generated Method Stub
    if (keycode==keyevent.keycode_back) {
    if ((System.currenttimemillis ()-mexittime) >waittime) {
    Toast.maketext (This, "Press the Eject program again", Toast.length_short). Show ();
    Mexittime=system.currenttimemillis ();
    }
    else {
    System.exit (0);
    }
    return true;
    }
    Return Super.onkeydown (KeyCode, event);
    }

  3. The principle of its implementation is to take you two times when you click the Exit button. Two times the interval is less than 2000 milliseconds to realize the exit, when two times the time interval size 2000, give the user a press again to exit the hint. What do you think. It is not very convenient to do so. Of course, there is a place to pay attention to, that is, you want to implement this function in which activity to put this code, if you want to put too many pages, then you write a class, in the activity to inherit the class.

"Press again to exit" feature in Android

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.