Idea: Android Capture user button is in the OnKeyDown method, just to determine whether the user button is keycode_back that is the back button, the rest is to judge the two times click the time interval of the button.
The first implementation method:
Package com.example.clickexittest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import android.view.KeyEvent;
Import Android.widget.Toast; public class Mainactivity extends activity {private static final String TAG = MainActivity_Exit.class.getSimpl
Ename ();
Define a variable to identify whether to exit the private static Boolean isexit = false;
private static Handler Mhandler = new Handler () {@Override public void Handlemessage (msg) {
Super.handlemessage (msg);
Isexit = false;
}
};
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode _back) {exit ();
return true;
Return Super.onkeydown (KeyCode, event);
private void Exit () {if (!isexit) {isexit = true;
Toast.maketext (Getapplicationcontext (), "Press the back key again to exit the program", Toast.length_short). Show ();
Use handler delay to send Change status information mhandler.sendemptymessagedelayed (0, 2000);
else {LOG.E (TAG, "Exit Application");
This.finish ();
}
}
}
In the Exit method, the value of the isexit is first judged, and if False, true, the prompt is popped, and a message is issued after 2000 milliseconds (2 seconds) to restore the value to false in handler. If the back key is pressed again within 2 seconds of the message interval being sent, the exit method is executed again, when the Isexit value is already true, and the exit method is executed.
The second way to implement it:
Package com.example.clickexittest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import android.view.KeyEvent;
Import Android.widget.Toast; public class Mainactivity extends activity {private static final String TAG = MainActivity.class.getSimpleName ()
; Private long clicktime = 0; Record the time of the first click @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinsta
Ncestate);
Setcontentview (R.layout.activity_main); @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_b
ACK) {exit ();
return true;
Return Super.onkeydown (KeyCode, event); private void Exit () {if (System.currenttimemillis ()-Clicktime) >) {toast.maketext (
Getapplicationcontext (), "Press the back key again to exit the program", Toast.length_short). Show (); ClicktIME = System.currenttimemillis ();
else {LOG.E (TAG, "Exit Application");
This.finish ();
System.exit (0);
}
}
}
Judge the user two times whether the time difference is within an expected value, is a direct exit directly, not the words prompt the user to press the back key again exit.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.