天天看点

【自用】双击返回键退出

/*============
 * 双击退出
 */
private static Boolean isExit = false;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        exitBy2Click(); //调用双击退出函数
    }
    return false;
}

private void exitBy2Click() {
    Timer tExit;
    if (!isExit) {
        isExit = true;
        Toast.makeText(this, "再按一次退出游戏", Toast.LENGTH_LONG).show();
        tExit = new Timer();
        tExit.schedule(new TimerTask() {
            @Override
            public void run() {
                isExit = false; // 取消
            }
        }, 2000); //2000ms没按就取消

    } else {
        finish();
        System.exit(0);
    }
}