The android program shields the return key from exiting, but supports the exit of the Return key with a long press.

Source: Internet
Author: User

Due to customer requirements, you need to block the return key and exit function of the program. However, because the program always needs an exit method, I use the long-pressed return key to exit.

At the beginning, I found the implementation in onkeydown on the Internet. But I found that in the real machine, although I long press the return key to make the program invisible, but cannot let the program exit.

Public Boolean onkeydown (INT kcode, keyevent kevent ){
Switch (kcode ){
Case keyevent. keycode_dpad_left :{
Return true;
}

Case keyevent. keycode_dpad_up :{
Return true;
}

Case keyevent. keycode_dpad_right :{
Return true;
}

Case keyevent. keycode_dpad_down :{
Return true;
}
Case keyevent. keycode_dpad_center :{
Return true;
}
Case keyevent. keycode_back :{
Return false;
}
}
Return super. onkeydown (kcode, kevent );
}

Last modified to implement the function of permanently exiting the long-pressed program.

Public Boolean dispatchkeyevent (keyevent event)
{
Int keycode = event. getkeycode ();
Switch (keycode)
{
Case keyevent. keycode_back :{
If (event. islongpress ())
{
This. stopservice (intent );
System. Exit (0 );
Return true;
} Else
{
Boolean flag = false;
Return flag;
}
}
}
Return super. dispatchkeyevent (event );

}

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.