// 点击返回键两次退出
private boolean isExit = false;
private long exitTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
// doubleClickExit();
doubleClickExit2();
return true;
}
return super.onKeyDown(keyCode, event);
}
/** 双击后退键退出 */
public void doubleClickExit() {
Timer exit = null;
if (isExit == false) {
isExit = true;
Toast.makeText(MainActivity.this, "再按一次返回到桌面", 0).show();
exit = new Timer();
exit.schedule(new TimerTask() {
@Override
public void run() {
isExit = false;
}
}, 2000);
} else {
finish();
System.exit(0);
}
}
private void doubleClickExit2() {
// System.currentTimeMillis()无论何时调用,肯定大于2000
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次返回键返回到桌面",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
}