Android重复点击后退,退出app,后台继续运行

  public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
        	
         if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
        	exit();
         }
        }
        
        return super.dispatchKeyEvent(event);
    }
 
    public void exit() {
        
        if (!isExit) {
          isExit = true;
            Toast.makeText(getApplicationContext(), "再点击一次退出",
                    Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
        	moveTaskToBack(true);
            //finish();
            //System.exit(0);
       }
    }

    private static Boolean isExit = false; 
 
    Handler mHandler = new Handler() {
 
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

你可能感兴趣的:(Mobile开发)