Android两次返回键退出APP(总结)

//退出时的时间
private long mExitTime;
//对返回键进行监听
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

        exit();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

public void exit() {
    if ((System.currentTimeMillis() - mExitTime) > 2000) {
        Toast.makeText(MainActivity.this, "再按一次退出当前应用程序", Toast.LENGTH_SHORT).show();
        mExitTime = System.currentTimeMillis();
    } else {
        finish();
        System.exit(0);
    }
}

你可能感兴趣的:(Android开发)