How to exit the program by clicking the return key for android
How to exit the program by clicking the return key for android
First, press the return key again to exit the program.
Private long exitTime = 0;
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK & event. getAction () = KeyEvent. ACTION_DOWN ){
If (System. currentTimeMillis ()-exitTime)> 2000 ){
Toast. makeText (getApplicationContext (), "exit the program once again", Toast. LENGTH_SHORT). show ();
ExitTime = System. currentTimeMillis ();
} Else {
Finish ();
System. exit (0 );
}
Return true;
}
Return super. onKeyDown (keyCode, event );
}
Or
Private long exitTime = 0;
@ Override
Public boolean dispatchKeyEvent (KeyEvent event ){
If (event. getKeyCode () = KeyEvent. KEYCODE_BACK
& Event. getAction () = KeyEvent. ACTION_DOWN
& Event. getRepeatCount () = 0 ){
If (System. currentTimeMillis ()-exitTime)> 2000 ){
Toast. makeText (getApplicationContext (), "exit the program again! ", Toast. LENGTH_SHORT). show ();
ExitTime = System. currentTimeMillis ();
} Else {
Finish ();
System. exit (0 );
}
Return true;
}
Return super. dispatchKeyEvent (event );
}
Type 2: Click the return key to bring up the confirmation window and select exit.
@ Override
Public boolean dispatchKeyEvent (KeyEvent event ){
If (event. getKeyCode () = KeyEvent. KEYCODE_BACK
& Event. getAction () = KeyEvent. ACTION_DOWN
& Event. getRepeatCount () = 0 ){
// Specific operation code
New AlertDialog. Builder (this)
. SetTitle ("are you sure you want to exit the program ")
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog, int whichButton ){
Finish ();//
}
}). Show ();
Return true;
}
Return super. dispatchKeyEvent (event );
}