第一步,定义一个变量,用于记录上一次按返回争键时间
private long lastBackKeyTime;
第二步,重写Activity中onKeyDown方法
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: long delay = Math.abs(System.currentTimeMillis() - lastBackKeyTime); if (delay > 3000) { // 双击退出程序 Toast toast = Toast.makeText(getApplicationContext(), "再按一次 退出系统", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); lastBackKeyTime = System.currentTimeMillis(); return true; } break; default: break; } return super.onKeyDown(keyCode, event); }
时间: 2024-10-09 18:58:41