[转]android两次按返回键退出程序实现

  1. private static Boolean isExit = false;  
  2.     private static Boolean hasTask = false;  
  3.     Timer tExit = new Timer();  
  4.     TimerTask task = new TimerTask() {  
  5.            
  6.         @Override  
  7.         public void run() {  
  8.             isExit = false;  
  9.             hasTask = true;  
  10.         }  
  11.     };  
  12.   
  13.   
  14. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  15.                 // TODO Auto-generated method stub  
  16.                 if(keyCode == KeyEvent.KEYCODE_BACK){  
  17. //                        System.out.println("user back down");  
  18.                         if(isExit == false ) {  
  19.                                 isExit = true;  
  20.                                 Toast.makeText(this"再按一次退出程序", Toast.LENGTH_SHORT).show();  
  21.                                 if(!hasTask) {  
  22.                                         tExit.schedule(task, 2000);  
  23.                                 }} else {  
  24.                                                                                 }  
  25.                                 finish();  
  26.                                 System.exit(0);  
  27.                         }  
  28.                 }                          
  29.                 return false;  
  30.         }  

你可能感兴趣的:(android)