连续点击返回键,退出应用程序

今天试了一下,比较常用的退出app方式,快速连击返回键,退出应用程序,不多说,上代码:

	// 退出时间
	private long currentBackPressedTime = 0;
	// 退出间隔
	private static final int BACK_PRESSED_INTERVAL = 2000;

	//在activity中重写onBackPressed方法
	@Override
	public void onBackPressed() {
		// 判断时间间隔
		if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
			currentBackPressedTime = System.currentTimeMillis();
			Toast.makeText(this, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
		} else {
			// 退出
			SysApplication.Quit(this);
		}
	}


 另一种方法是:重写onKeyDown方法,里面写一个popupwindow展示

	@Override
	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		switch (paramInt) {
		case 4:
			PopupUtil.showOutPop(this, this.homePageMainLayout);
		default:
			return false;
		}

	}


 

原文地址:https://www.cnblogs.com/riskyer/p/3400117.html