Android implementation long press the back key to exit the application method _android

Source: Internet
Author: User

The example in this article describes how the Android implementation long presses the back key to exit the application. Share to everyone for your reference. The specific analysis is as follows:

One of the most recent applications on Android is how to implement a long press back key to exit the application. Find a lot of information on the Internet, found that almost no such implementation, most of the processing is to double-click the back key to exit the application. A reference to double-click the back key to exit the application code, online mainstream of a method is the following method, to achieve relatively simple:

@Override Public
boolean onKeyDown (int keycode, keyevent event) 
{
  if (keycode = = Keyevent.keycode_back) 
  {
   if (System.currenttimemillis ()-mexittime) >) 
   {
    mhelperutils.showtoast (Mcontext, r.string.exit_content);
    Mexittime = System.currenttimemillis ();
   }
   else 
   {
    finish ();
   }
   return true;
  }
  Return Super.onkeydown (KeyCode, event);
}

From the above code can be seen, the use of the idea is two consecutive clicks on the back key time interval within 2s, it is considered to be double click, and then quit the program.

Therefore, from the above ideas, my initial idea is that you can rewrite the OnKeyDown method and the OnKeyUp method, calculate the interval between the two methods, if greater than 2s, it is considered to be a long press, thereby exiting the program, otherwise, perform the original response of the back key. In this way:

public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = KEYEVENT.KEYCODE_BACK&&EVENT.GETREPEATC 
  Ount () ==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.getrepeatcou 
  NT () ==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 ("Confirm exit"?)
  ");
  Builder.settitle ("hint"); Builder.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,in
   T which) {Dialog.dismiss ();
   MainActivity.this.finish ();
   System.exit (0);
  }
  }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,in T WHich) {Dialog.dismiss ();
  }
  });
  Builder.create (). Show ();
  return true;
  else {return Super.onkeyup (KeyCode, event);
} return Super.onkeyup (KeyCode, event); }

However, this enables the long press the back key to exit the program, but when the back key is not long, the Click Back is invalid, looking for a lot of Android button event distribution and processing mechanism information, or do not know why (there is time to study later). Then consider a different way of thinking to deal with, consider rewriting the activity of the Dispatchkeyevent method.

 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 sentence is important to determine whether the event is long by event {Alertdialog.builder
  Builder = new Builder (mainactivity.this); Builder.setmessage ("Confirm exit"?)
  ");
  Builder.settitle ("hint"); Builder.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,in
   T which) {Dialog.dismiss ();
   MainActivity.this.finish ();
   System.exit (0);
  }
  }); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,in
   T which) {Dialog.dismiss ();
  }
  });
  Builder.create (). Show ();
  return true;
  Return Super.dispatchkeyevent (event);
 If it is not a long press, the original method is invoked to perform the processing default:break the back key should be pressed;
Return Super.dispatchkeyevent (event); }

Finally, by rewriting the Dispatchkeyevent method to achieve a long press the back key to exit the program, and did not block the original not long press the back key function.

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.