android单击返回键两次退出

  1. 需求:单击返回键两次退出,单击一次返回键Toast提示,在其后2s内再单击一次返回键就退出。
  2.        private static Boolean isExit = false;
  3.        private static Boolean hasTask = false;
  4.         Timer tExit = new Timer();
  5.         TimerTask task = new TimerTask() {
  6.                 @Override
  7.                 public void run() {
  8.                         isExit = false;
  9.                         hasTask = true;
  10.                 }
  11.         };
  12.         @Override
  13.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  14.                 if (keyCode == KeyEvent.KEYCODE_BACK) {
  15.                         if (isExit == false) {
  16.                                 isExit = true;
  17.                                 Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
  18.                                 if (!hasTask) {
  19.                                         tExit.schedule(task, 2000);
  20.                                 }
  21.                         } else {
  22.                                 finish();
  23.                                 System.exit(0);
  24.                         }
  25.                 }
  26.                 return false;
  27.         }
posted @ 2013-08-12 08:52  brave-sailor  阅读(169)  评论(0编辑  收藏  举报