Android double click return key exit application implementation method details

Time:2020-9-13

preface

Now many applications on the market will prompt users when they press the return key: press the return button again to exit the application, that is, double-click the return key to exit the application. Next, we will use several methods to achieve this function

design sketch

realization

The first method

Respond to the onkeyup event of activity. If the time of two clicks is more than 2 seconds, it will not respond. If it is less than 2 seconds, it will exit the program
code:

//Record the first time the user clicks the return key
 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 again to exit the program, Toast.LENGTH_ SHORT).show();
    firstTime = secondTime;
    return true;
   } else {
    System.exit(0);
   }
  }

  return super.onKeyUp(keyCode, event);
 }

The second way

As with the first method, this is in response to the onkeydown event
code:

/**
  *The second way
  * @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 override onbackpressed() method
code:

/**
  *The third method
  */
 @Override
 public void onBackPressed() {
  long secondTime = System.currentTimeMillis();
  if (secondTime - firstTime > 2000) {
   Toast.makeText ( MainActivity.this , press again to exit the program, Toast.LENGTH_ SHORT).show();
   firstTime = secondTime;
  } else {
   System.exit(0);
  }

The fourth way

Through the thread delay method, with the help of timer
code:

/**
  *The fourth method
  */
 @Override
 public void onBackPressed() {
  if (!mBackKeyPressed) {
   Toast.makeText (this, "press 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 way
  * @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, "press 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 way

Using timer within the onkeydown() method

/**
  *The sixth way
  * @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, "press 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);
 }

OK, that’s the end. The principle is similar.

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.