Get the App
SLTechnology News&Howtos  ›  Development  › 

How to exit the application by double-clicking the return key in Android

Shulou Source: shulou.com Published: 2022-06-03 09:38:39 09月20日 Update

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!

Tags: Methods programs methods code articles applications events time this is not value interest at the same time more friends users knowledge threads editing industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Technology Shulou Information NVidia Apple