In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Android how to double-click the return key to exit the application, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Realize
The first way
Respond to the onKeyUp event of Activity, do not respond after two clicks of more than 2 seconds, and exit the program code after less than 2 seconds:
/ / record the time when the user clicked the return key for the first time private long firstTime = 0; / * the first solution is to monitor keyUp * @ param keyCode * @ param event * @ return * / @ Override public boolean onKeyUp (int keyCode, KeyEvent event) {if (keyCode = = KeyEvent.KEYCODE_BACK & & event.getAction () = = KeyEvent.ACTION_UP) {long secondTime = System.currentTimeMillis () If (secondTime-firstTime > 2000) {Toast.makeText (MainActivity.this, "Press exit Program again", Toast.LENGTH_SHORT) .show (); firstTime = secondTime; return true;} else {System.exit (0);}} return super.onKeyUp (keyCode, event);}
The second way
As with the first approach, this is only this time in response to the onKeyDown event code:
/ * the second method * @ param keyCode * @ param event * @ return * / @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = = KeyEvent.KEYCODE_BACK & & event.getAction () = = KeyEvent.ACTION_DOWN) {long secondTime = System.currentTimeMillis (); if (secondTime-firstTime > 2000) {Toast.makeText (MainActivity.this, "press again to exit the program", Toast.LENGTH_SHORT). Show (); firstTime = secondTime Return true;} else {System.exit (0);}} return super.onKeyDown (keyCode, event);}
The third way
Directly rewrite the code for the onBackPressed () method:
/ * third method * / @ Override public void onBackPressed () {long secondTime = System.currentTimeMillis (); if (secondTime-firstTime > 2000) {Toast.makeText (MainActivity.this, "click again to exit the program", Toast.LENGTH_SHORT). Show (); firstTime = secondTime;} else {System.exit (0);}
The fourth way
This is achieved by thread delay, with the help of Timer code:
/ * fourth method * / @ Override public void onBackPressed () {if (! mBackKeyPressed) {Toast.makeText (this, "click again to exit the program, Toast.LENGTH_SHORT) .show (); mBackKeyPressed = true; new Timer () .schedule (new TimerTask () {@ Override public void run () {mBackKeyPressed = false;}}, 2000);} else {this.finish (); System.exit (0);}}
The fifth way
Using Timer within the onKeyUp () method
/ * the fifth method * @ param keyCode * @ param event * @ return * / @ Override public boolean onKeyUp (int keyCode, KeyEvent event) {if (keyCode = = KeyEvent.KEYCODE_BACK & & event.getAction () = = KeyEvent.ACTION_UP) {if (! mBackKeyPressed) {Toast.makeText (this, "click again to exit the program", Toast.LENGTH_SHORT) .show (); mBackKeyPressed = true New Timer () .schedule (new TimerTask () {@ Override public void run () {mBackKeyPressed = false;}}, 2000); return true;} else {this.finish (); System.exit (0);}} return super.onKeyUp (keyCode, event);}
The sixth method
Using Timer within the onKeyDown () method
/ * the sixth method * @ param keyCode * @ param event * @ return * / @ Override public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = = KeyEvent.KEYCODE_BACK & & event.getAction () = = KeyEvent.ACTION_DOWN) {if (! mBackKeyPressed) {Toast.makeText (this, "click again to exit the program", Toast.LENGTH_SHORT) .show (); mBackKeyPressed = true New Timer () .schedule (new TimerTask () {@ Override public void run () {mBackKeyPressed = false;}}, 2000); return true;} else {this.finish (); System.exit (0);}} return super.onKeyDown (keyCode, event) } Thank you for reading this article carefully. I hope the article "Android how to double-click the return key to exit the application" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.