Android再按一次后退键退出

boolean isEixt=false;
boolean hasExitTask=false;
Timer exitTimer=new Timer();
TimerTask exitTask=new TimerTask(){ 
	public void run() {   
		isExit = false;   
		hasExitTask = true;   
	}   
};

/**返回键拦截监听*/
public boolean onKeyDown(int keyCode, KeyEvent event) {   
	if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN) {   
		if(isExit){
			exit();
		}else{
			isExit=true;
			Toast.makeText(this, "快速再按一次后退键退出", Toast.LENGTH_SHORT).show();   
			if(!hasExitTask) {   
				exitTimer.schedule(exitTask, 3000);   
			}   
		}
	}
	return false;   
}

/**退出*/
void exit(){
	finish();
	java.lang.System.exit(0);
}

 

你可能感兴趣的:(android,timer)