Android app exit by pressing the back key
This article describes how to exit the application by pressing the back key for Android. Share it with you for your reference. The specific analysis is as follows:
Recently, when I was working on an Android Application, I encountered a problem: how to enable long-Press the back key to exit the application. I searched for a lot of information on the Internet and found that there was almost no such implementation. Most of the operations were performed by double-clicking the back key to exit the application. Refer to the Code for double-clicking the back key to exit the application. One of the mainstream methods on the Internet is the following method, which is easy to implement:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@ Override Public boolean onKeyDown (int keyCode, KeyEvent event) { If (keyCode = KeyEvent. KEYCODE_BACK) { If (System. currentTimeMillis ()-mExitTime)> 2000) { MHelperUtils. showToast (mContext, R. string. exit_content ); Mexico time = System. currentTimeMillis (); } Else { Finish (); } Return true; } Return super. onKeyDown (keyCode, event ); } |
From the code above, we can see that the adopted idea is to double-click the back key for two consecutive times within 2 seconds, and then exit the program.
Therefore, based on the above idea, my initial idea is: you can rewrite the onKeyDown method and the onKeyUp method to calculate the interval between the two methods. If the interval is greater than 2 s, long press is considered to exit the program; otherwise, the original response of the back key is executed. Follow these steps:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
Public boolean onKeyDown (int keyCode, KeyEvent event) { If (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0) { Start = System. currentTimeMillis (); Log. e ("start", String. valueOf (start )); Return false; } Return super. onKeyDown (keyCode, event ); } Public boolean onKeyUp (int keyCode, KeyEvent event) { If (keyCode = KeyEvent. KEYCODE_BACK & event. getRepeatCount () = 0) { End = System. currentTimeMillis (); Log. e ("end", String. valueOf (end )); If (start! =-1 & (end-start)> 2000) { AlertDialog. Builder builder = new Builder (MainActivity. this ); Builder. setMessage ("are you sure you want to exit? "); Builder. setTitle ("prompt "); Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener () { Public void onClick (DialogInterface dialog, int which) { Dialog. dismiss (); MainActivity. this. finish (); System. exit (0 ); } }); Builder. setNegativeButton ("cancel", new DialogInterface. OnClickListener () { Public void onClick (DialogInterface dialog, int which) { Dialog. dismiss (); } }); Builder. create (). show (); Return true; } Else { Return super. onKeyUp (keyCode, event ); } } Return super. onKeyUp (keyCode, event ); } |
However, in this way, you can exit the program by pressing the back key for a long time. However, if you do not press the back key for a long time, clicking back becomes invalid. You have found a lot of information about Android key event distribution and processing mechanisms, I still haven't figured out the reason (I have time to study it later ). Therefore, we should consider another way of thinking, and rewrite the dispatchKeyEvent method in the Activity.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Public boolean dispatchKeyEvent (KeyEvent event) { Int keyCode = event. getKeyCode (); // Log. e ("start", String. valueOf (start )); Switch (keyCode) { Case KeyEvent. KEYCODE_BACK: If (event. isLongPress ()) // This sentence is very important. Determine whether the event is a long-pressed event. { AlertDialog. Builder builder = new Builder (MainActivity. this ); Builder. setMessage ("are you sure you want to exit? "); Builder. setTitle ("prompt "); Builder. setPositiveButton ("OK", new DialogInterface. OnClickListener () { Public void onClick (DialogInterface dialog, int which) { Dialog. dismiss (); MainActivity. this. finish (); System. exit (0 ); } }); Builder. setNegativeButton ("cancel", new DialogInterface. OnClickListener () { Public void onClick (DialogInterface dialog, int which) { Dialog. dismiss (); } }); Builder. create (). show (); Return true; } Return super. dispatchKeyEvent (event ); // If it is not a long press, the original method is called and the corresponding processing is performed by pressing the back key. Default: Break; } Return super. dispatchKeyEvent (event ); } |
At last, the dispatchKeyEvent method is rewritten to achieve long-press back to exit the program, and the original non-Long-press back function is not blocked.
I hope this article will help you design your Android program.