当前位置:主页 > 架构说 >
架构说
联系我们

android 双击退出

文章出处:凯发K 发表时间:2019-02-01

android 双击退出

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(!exitBy2Click(HomeActivity.this)){
				return false;
			}
			 // 调用双击退出函数
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private long time;

	private boolean exitBy2Click(final Context context) {
		if(System.currentTimeMillis()-time>2000){//大于2秒
			Toast.makeText(context, "再按一下退出应用", Toast.LENGTH_SHORT).show();
			time = System.currentTimeMillis();
			Handler handler = new Handler();
			handler.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					time = 0;
					
				}
			}, 2000);
			return false;
		}else{//两秒内。退出应用
			return true;
		}
		
		
	}
貌似没有什么bug 吧

http://www.bkjia.com/Androidjc/872946.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/872946.htmlTechArticleandroid 双击退出 @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if(!exitBy2Click(HomeActivity.this)){return false;} // 调...

本文源自: 环亚娱乐