按 Back 兩次才關閉程式

1 篇文章 / 0 new
author
按 Back 兩次才關閉程式
private long exitTime = 0;
 
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Log.d("keyCode:",String.format("%1$d", keyCode));
    // 可藉此攔截來避免程式被離開
    if (keyCode == 4) {// back key
        ExitApp();
        return false;
    } else {// other key
        return true;
    }
}
 
public void ExitApp() {
    if ((System.currentTimeMillis() - exitTime) > 2000) {
        Toast.makeText(this, "再按一次退出程式", Toast.LENGTH_SHORT).show();
        exitTime = System.currentTimeMillis();
    } else {
        finish();
    }
}
Free Web Hosting