Android再按一次退出

long exitTime = 0;//再点一次退出变量6
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK) {
      // Toast.makeText(MainActivity.this, "返回键无效", Toast.LENGTH_SHORT).show();
       if ((System.currentTimeMillis() - exitTime) > 2000) {  //System.currentTimeMillis()无论何时调用,肯定大于2000
           Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
           exitTime = System.currentTimeMillis();
       } else {
           finish();
           System.exit(0);
       }   
       return true;//return true;拦截事件传递,从而屏蔽back键。
   }
   if (KeyEvent.KEYCODE_HOME == keyCode) {
       Toast.makeText(getApplicationContext(), "HOME 键已被禁用...", Toast.LENGTH_SHORT).show();
       return true;//同理
   }
   return super.onKeyDown(keyCode, event);
}

你可能感兴趣的:(android)