How does Android capture the mobile phone back key (back) to exit the program?

Source: Internet
Author: User

[Bloggers: Dear bloggers, netizens, and everyone else! Welcome to this blog. You are welcome to have more exchanges, give more comments, learn from each other, and make mutual progress. Our slogan is: study hard and make progress every day .]

This application is widely used. Basically every application needs it. When you click the back key on your mobile phone, a dialog box is displayed, and you can select "OK" or "cancel ".

 

 

Next, let's talk about how the program captures the back key. When the user clicks the back key, the execution program will be triggered.

Capture method:

/** <Br/> * capture back <br/> */<br/> @ override <br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_back & event. getrepeatcount () = 0) {<br/> exitdialog (mainactivity. this ). show (); <br/> return true; <br/>}</P> <p> return Super. onkeydown (keycode, event); <br/>}

 

The pop-up dialog implementation is as follows:

/** <Br/> * The system exits. <br/> * @ Param context <br/> * @ return <br/> */<br/> private dialog exitdialog (context context) {<br/> alertdialog. builder = new alertdialog. builder (context); <br/> builder. seticon (R. drawable. icon); <br/> builder. settitle ("system information"); <br/> builder. setmessage ("are you sure you want to exit the program? "); <Br/> builder. setpositivebutton ("OK", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface dialog, int whichbutton) {<br/> finish (); <br/>}< br/> }); <br/> builder. setnegativebutton ("cancel", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface dialog, int whichbutton) {<br/>}< br/>}); <br/> return builder. create (); <br/>}

 

 

 

Application is very simple, hope to help you, reprint Please note: http://blog.csdn.net/richway2010

 

 

 

 

 

 

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.