Android two-time backend and android two-time backend
Reprinted please indicate the source: http://blog.csdn.net/javacattle/article/details/41964045
You only need to add it to the *. Java file.
Private int back = 0;
// ======================== Back two exits =========================== ==@ Override public boolean onCreateOptionsMenu (Menu menu) {// you must change the menu. add (0, 2, 2, "quit"); return super. onCreateOptionsMenu (menu );}
@ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case 2: Myback (); break;} return super. onOptionsItemSelected (item );}
@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// two back exits if (keyCode = KeyEvent. KEYCODE_BACK) {back ++; switch (back) {case 1: Toast. makeText (MainActivity. this, "exit once again", Toast. LENGTH_SHORT ). show (); break; case 2: back = 0; // initialize the back value Myback (); break;} return true; // set it to false to invalidate the back, true indicates no expiration} else {return super. onKeyDown (keyCode, event );}}
Public void Myback () {// close the MainActivity program. this. finish (); android. OS. process. killProcess (android. OS. process. myPid (); // closes the process}