Android Continuous Click to exit the program

Source: Internet
Author: User

Package com.test.twiceexit;

Import Java.util.Timer;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.view.KeyEvent;
Import Android.view.Menu;
Import Android.widget.Toast;


public class Mainactivity extends Activity {
private static Boolean Isquit = false;
Private long mexittime = 0;

Timer timer = new timer ();

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

/**************************************************
* Method One
* ******************************************** /
//@Override
//public boolean onKeyDown (int keycode, keyevent event) {
//if (keycode = = Keyevent.keycode_ back) {
//if (Isquit = = False) {
//isquit = true;
Toast.maketext (Getbasecontext (), "Press the return key again to exit the program",
//toast.length_short). Show ();
TimerTask task = null;
//task = new TimerTask () {
//public void Run () {
//isquit = false;
}
//};
//timer.schedule (Task, 2000);
//} else {
//finish ();
System.exit (0);
//}
//}
//return false;
//}

/**************************************************
* Method Two
* ************************************************/
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
if ((System.currenttimemillis ()-Mexittime) > 2000) {//
Do not exit if the two keystroke interval is greater than 2000 milliseconds
Toast.maketext (This, "Press again to exit the program", Toast.length_short). Show ();
Mexittime = System.currenttimemillis ();//Update Mexittime
} else {
System.exit (0);//Exit the program
//}
return true;
//}
Return Super.onkeydown (KeyCode, event);
//
//}

/**************************************************
* Method Three
* ************************************************/
Handler Mhandler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Isquit = false;
}
};

@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
if (!isquit) {
Isquit = true;
Toast.maketext (Getapplicationcontext (), "Press again to exit the program",
Toast.length_short). Show ();
Using handler delay to send Change status information
Mhandler.sendemptymessagedelayed (0, 2000);
} else {
Finish ();
System.exit (0);
}
}
return false;
}
}

Android Continuous Click to exit the program

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.