点击两次返回键程序退出

//连续两次返回退出程序
		private long exitTime = 0;  
		@Override  
		public boolean onKeyDown(int keyCode, KeyEvent event) {  
		    if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){                    
		    if((System.currentTimeMillis()-exitTime) > 2000){  
		        Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();                                exitTime = System.currentTimeMillis();  
		    }  
		    else{  
		        finish();  
		        System.exit(0);  
		        }  
                  
		    return true;  
		    }  
		    return super.onKeyDown(keyCode, event);  
		}  //End of 连续两次返回退出程序








privatestatic Boolean isExit =false;  
privatestatic Boolean hasTask =false;
Timer tExit
=new Timer();
TimerTask task
=new TimerTask() {

@Override
publicvoid run() {
isExit
=false;
hasTask
=true;
}
};


publicboolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK){
// System.out.println("user back down");
if(isExit ==false ) {
isExit
=true;
Toast.makeText(
this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
if(!hasTask) {
tExit.schedule(task,
2000);
}}
else {
}
finish();
System.exit(
0);
}
}
returnfalse;
}

posted on 2012-07-11 15:38  jiezzy  阅读(427)  评论(0编辑  收藏  举报