首页 > 代码库 > android_双击退出

android_双击退出

/**
	 * 设置快速双击退出程序
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exitBy2Click();
		}
		return false;
	}

	/**
	 * 双击退出函数 zhaoyazhi
	 */
	private void exitBy2Click() {
		// TODO Auto-generated method stub
		Timer tExit = null;
		if (isExit == false) {
			isExit = true;// 准备退出
			Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false;// 取消退出
				}
			}, 2000);// 如果2秒没有按下返回键则启动定时器取消掉刚才执行的任务
		} else {
			// 结束所有的界面和后台程序
			AccessTokenKeeper.clear(getApplication());
			ActivityManager activityMgr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
			activityMgr.restartPackage(getPackageName());
			MyApp.getInstance().exit();
			System.exit(0);
			finish();
		}
	}

android_双击退出