Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize double-click exit in android

Shulou Source: shulou.com Published: 2022-05-31 21:45:45 09月15日 Update

This article mainly explains "how to achieve double-click exit in android". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve double-click exit in android".

Using thread delay to realize android double-click exit

PrivatestaticbooleanmBackKeyPressed=false;// record whether the first key @ Override publicvoidonBackPressed () {if (! mBackKeyPressed) {Toast.makeText (this, "press again to exit the program, Toast.LENGTH_SHORT) .show (); mBackKeyPressed=true; newTimer () .schedule (newTimerTask () {/ / delay two seconds, if it exceeds, the first key record @ Override publicvoidrun () {mBackKeyPressed=false;}}, 2000) } else {/ / exit the program this.finish (); / / System.exit (0);}}

Record whether the return key is pressed for the first time through mBackKeyPressed, and if there is no record of the first keystroke, Toast prompt, and record the first keystroke record, and start the thread to erase the keystroke record after 2 seconds. If the return key is pressed when the mBackKeyPressed is erased in the thread, the statement in the else is executed to exit the program.

Double-click exit of android by calculating time difference

PrivatelongmPressedTime=0; @ Override publicvoidonBackPressed () {longmNowTime=System.currentTimeMillis (); / / get the first keystroke time if ((mNowTime-mPressedTime) > 2000) {/ / compare the time difference between two keystrokes Toast.makeText (this, "press again", Toast.LENGTH_SHORT). Show (); mPressedTime=mNowTime;} else {/ / exit program this.finish (); System.exit (0) }} Thank you for your reading, the above is the content of "how to achieve double-click exit in android". After the study of this article, I believe you have a deeper understanding of how to achieve double-click exit in android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Keys programs time learning content time difference for the first time threads that is ideas situations articles more knowledge knowledge points articles sentences follow questions Cheng Hai Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Huawei Docker vpn