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 );
}