Android implements the long press back key to exit the application

  • 2020-06-19 11:43:00
  • OfStack

This article illustrated the Android implementation of long pressing the back key to exit an application. Share to everybody for everybody reference. The specific analysis is as follows:

Recently, when I was working on an application on Android, I encountered a problem that how to achieve long press back key to exit the application. I looked up a lot of information on the Internet and found that there were few such implementations, most of which were double-clicking the back key to exit the application. Referring to the code of double-clicking back key to exit the application, one of the mainstream methods on the Internet is the following one, which is relatively simple to implement:


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
  if (keyCode == KeyEvent.KEYCODE_BACK) 
  {
   if ((System.currentTimeMillis() - mExitTime) > 2000) 
   {
    mHelperUtils.showToast(mContext, R.string.exit_content);
    mExitTime = System.currentTimeMillis();
   }
   else 
   {
    finish();
   }
   return true;
  }
  return super.onKeyDown(keyCode, event);
}

As can be seen from the above code, the idea is to click the back key twice in a row within the time interval of 2s, which is considered a double click to exit the program.

Therefore, based on the above train of thought, my initial idea is that I can rewrite onKeyDown method and onKeyUp method to calculate the time interval between the execution of these two methods. If it is greater than 2s, it will be considered as a long press to exit the program. Otherwise, the original response of the back key is executed. Follow this line of thought:


public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
 if (keyCode == KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0) 
 { 
  start=System.currentTimeMillis(); 
  Log.e("start",String.valueOf(start));
  return false; 
 } 
 return super.onKeyDown(keyCode, event);
} 
public boolean onKeyUp(int keyCode,KeyEvent event)
{
 if (keyCode == KeyEvent.KEYCODE_BACK &&event.getRepeatCount()==0) 
 { 
  end=System.currentTimeMillis(); 
  Log.e("end",String.valueOf(end));
  if(start!=-1&&(end-start)>2000)
  {
  AlertDialog.Builder builder = new Builder(MainActivity.this);
  builder.setMessage(" Do you want to confirm your exit? ");
  builder.setTitle(" prompt ");
  builder.setPositiveButton(" confirm ",new DialogInterface.OnClickListener()
  {
   public void onClick(DialogInterface dialog,int which)
   {
   dialog.dismiss();
   MainActivity.this.finish();
   System.exit(0);
   }
  });
  builder.setNegativeButton(" cancel ",new DialogInterface.OnClickListener()
  {
   public void onClick(DialogInterface dialog,int which) 
   {
   dialog.dismiss();
   }
  });
  builder.create().show();
  return true;
  }
  else
  {
  return super.onKeyUp(keyCode, event);
  }
 }
 return super.onKeyUp(keyCode, event);
}

However, long press back to exit the program, but not long press back, click back will not work, looking at a lot of Android key event distribution and processing mechanism, but still do not know why (more time later). So I consider another way to deal with it, and consider rewriting the dispatchKeyEvent method in Activity.


public boolean dispatchKeyEvent(KeyEvent event)
{
 int keyCode=event.getKeyCode();
 //Log.e("start",String.valueOf(start));
 switch(keyCode)
 {
  case KeyEvent.KEYCODE_BACK:
  if(event.isLongPress())
  // This is an important sentence to determine if an event is a long-press event 
  {
  AlertDialog.Builder builder = new Builder(MainActivity.this);
  builder.setMessage(" Do you want to confirm your exit? ");
  builder.setTitle(" prompt ");
  builder.setPositiveButton(" confirm ",new DialogInterface.OnClickListener()
  {
   public void onClick(DialogInterface dialog,int which)
   {
   dialog.dismiss();
   MainActivity.this.finish();
   System.exit(0);
   }
  });
  builder.setNegativeButton(" cancel ",new DialogInterface.OnClickListener() 
  {
   public void onClick(DialogInterface dialog,int which) 
   {
   dialog.dismiss();
   }
  });
  builder.create().show();
  return true;
  }
  return super.dispatchKeyEvent(event);
  // If it is not a long press, the original method is called and executed by pressing back The key should be handled 
  default:
  break;
 }
 return super.dispatchKeyEvent(event);
}

Finally, long press back key to exit the program was achieved by rewriting dispatchKeyEvent method, and the original function of non-long press back key was not screened.

Hopefully, this article has helped you with your Android programming.


Related articles: