萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> android中的按兩次返回鍵退出代碼

android中的按兩次返回鍵退出代碼

   代碼如下

  private long exitTime;

  @Override

  public boolean onKeyDown(int keyCode, KeyEvent event) {

  if (keyCode == KeyEvent.KEYCODE_BACK

  && event.getAction() == KeyEvent.ACTION_DOWN) {

  if ((System.currentTimeMillis() - exitTime) > 2000) // System.currentTimeMillis()無論何時調用,肯定大於2000

  {

  Toast toast = Toast.makeText(getApplicationContext(),

  getResources().getString(R.string.press_again_exit),

  Toast.LENGTH_SHORT);

  toast.setGravity(Gravity.CENTER, 0, 0);

  toast.show();

  exitTime = System.currentTimeMillis();

  } else {

  finish();

  System.exit(0);

  }

  return true;

  }

  return super.onKeyDown(keyCode, event);

  }

copyright © 萬盛學電腦網 all rights reserved