Android simple App (ii)--use dispatchkeyevent double exit program

Source: Internet
Author: User

The key operation of the Android system is first processed in Dispatchkeyevent, then onkeydown (int keycode, keyevent event) and onkeyup (int keycode, keyevent Event).

In the same vein, touch operations are processed first in dispatchtouchevent and distributed to ontouchevent.

The code is simple, so long as you have a good command of the event's distribution order is OK.
Code in activity:

@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        //TODO auto-generated Method StubLOG.D ("CPACM", "OnKeyDown"); return Super. OnKeyDown (KeyCode, event); } @Override Public BooleanOnKeyUp (intKeyCode, KeyEvent event) {        //TODO auto-generated Method StubLOG.D ("CPACM", "OnKeyUp"); return Super. OnKeyUp (KeyCode, event); } @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method StubLOG.D ("CPACM", "Ontouchevent"); return Super. Ontouchevent (event); } @Override Public Booleandispatchkeyevent (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, "Press again to exit the program", Toast.length_short). Show ();
//system.exit (0); Pretime=System.currenttimemillis (); return true; } //TODO auto-generated Method Stub return Super. Dispatchkeyevent (event); } @Override Public Booleandispatchtouchevent (motionevent ev) {//TODO auto-generated Method StubLOG.D ("CPACM", "Dispatchtouchevent"); return Super. dispatchtouchevent (EV); }

By the way, a piece ~

Android simple App (ii)--use dispatchkeyevent double exit program

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.