Android completely disables the capture of the app's return key

Source: Internet
Author: User
When developing Android applications, you often press the return key (that is, keycode = keyevent. keycode_back) to close the program. In most cases, the application is still running in the task. In fact, this is not the result we want.
We can do this. When a user clicks the custom exit button or return key (capture action is required), We forcibly exit the application in ondestroy () or directly kill the process, the specific operation code is as follows:
 

 
@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){

// Press the return button on the keyboard
If (keycode = keyevent. keycode_back ){
 
New alertdialog. Builder (this)
. Seticon (R. drawable. Services)
. Settitle (R. String. Prompt)
. Setmessage (R. String. quit_desc)
. Setnegativebutton (R. String. Cancel, new dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
}
})
. Setpositivebutton (R. String. Confirm, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
Finish ();
}
}). Show ();

Return true;
} Else {
Return super. onkeydown (keycode, event );
}
}

 
 
@ Override
Protected void ondestroy (){
Super. ondestroy ();

System. Exit (0 );
// Or the following method
// Android. OS. process. killprocess (Android. OS. process. mypid ());
}
 

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.