Android clicks the return key to exit the program. android returns
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 );
}
In the Android program, press the return key to exit the program. I want to press the return key to return to the previous interface. How should I implement this?
If you use setcontentview, that is, the activity is not actually switched, you have to handle the event that returns the key by yourself. When you click return, setcontentview is the one on the previous page.
How can I exit a program by pressing the return key in Android?
Development ????
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
// TODO Auto-generated method stub
Switch (keyCode ){
Case KeyEvent. KEYCODE_BACK:
AlertDialog. Builder build = new AlertDialog. Builder (this );
Build. setTitle ("NOTE ")
. SetMessage ("are you sure you want to exit? ")
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
Finish ();
}
})
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
}
})
. Show ();
Break;
Default:
Break;
}
Return false;
// Return super. onKeyDown (keyCode, event );
}