Android 点击back键两次退出程序

    Android中为了界面上的简洁,可以通过点击两次back键退出程序,而不用退出按钮。ok,直接上代码

private long exitTime = 0;
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   ExitApp();
   return false;
 }
 public void ExitApp() {
   if ((System.currentTimeMillis() - exitTime) > 2000) {
    Toast.makeText(BackAppActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT)
      .show();
    exitTime = System.currentTimeMillis();
   } else {
    finish();
   }
 }

 

你可能感兴趣的:(android,退出,back)