Simple Application of Android (2) -- double-click dispatchKeyEvent to exit the program and dispatchkeyevent

Source: Internet
Author: User

Simple Application of Android (2) -- double-click dispatchKeyEvent to exit the program and dispatchkeyevent

In the Android system, key operations are processed in dispatchKeyEvent first, and then onKeyDown (int keyCode, KeyEvent event) and onKeyUp (int keyCode, KeyEvent event ).

Similarly, the touch operation is first processed in the dispatchTouchEvent and then processed in the distribution to the onTouchEvent.

The code is very simple, as long as you have a good grasp of the event distribution order is OK.
Code in Activity:

@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stub Log. d ("CPACM", "onKeyDown"); return super. onKeyDown (keyCode, event) ;}@ Override public boolean onKeyUp (int keyCode, KeyEvent event) {// TODO Auto-generated method stub Log. d ("CPACM", "onKeyUp"); return super. onKeyUp (keyCode, event) ;}@ Override public boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stub Log. d ("CPACM", "onTouchEvent"); return super. onTouchEvent (event) ;}@ Override public boolean dispatchKeyEvent (KeyEvent event) {Log. d ("CPACM", "dispatchKeyEvent"); if (event. getKeyCode () = KeyEvent. KEYCODE_BACK & event. getAction () = KeyEvent. ACTION_DOWN) {if (System. currentTimeMillis ()-preTime <2000) {// within two seconds, exit return super. dispatchKeyEvent (event );}
Toast. makeText (this, "exit the program again", Toast. LENGTH_SHORT). show ();
// System. exit (0); preTime = System. currentTimeMillis (); return true;} // TODO Auto-generated method stub return super. dispatchKeyEvent (event) ;}@ Override public boolean dispatchTouchEvent (MotionEvent ev) {// TODO Auto-generated method stub Log. d ("CPACM", "dispatchTouchEvent"); return super. dispatchTouchEvent (ev );}

 

By the way, a copy is provided ~

 

 

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.