Android 如何監(jiān)聽返回鍵點(diǎn)擊事件,并創(chuàng)建雙擊返回鍵退出程序,
注:如下代碼當(dāng)時(shí)是從網(wǎng)上copy過來的。來源:http://blog.csdn.net/mrlixirong/article/details/6854292
boolean isEixt=false;
boolean hasExitTask=false;
Timer exitTimer=new Timer();
TimerTask exitTask=new TimerTask(){
public void run() {
isEixt = false;
hasExitTask = true;
}
};
/**返回鍵攔截監(jiān)聽*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN) {
if(isEixt){
exit();
}else{
isEixt=true;
Toast.makeText(this, "快速再按一次后退鍵退出", Toast.LENGTH_SHORT).show();
if(!hasExitTask) {
exitTimer.schedule(exitTask, 3000);
}
}
}
return false;
}
/**退出*/
void exit(){
finish();
java.lang.System.exit(0);
}
發(fā)表評(píng)論