Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize double-click exit in android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report