/** * 两秒之内,按两次回退键才会推出app */ public static boolean isToFinish = false; public static boolean isFirst = true; public static boolean finishTwoBack(Context mContext){ Handler handler = new Handler(); Runnable r = new Runnable() { @Override public void run() { isFirst = true; isToFinish = false; } }; if(isFirst){ isFirst = false; handler.postDelayed(r, 2000); Toast.makeText(mContext, "再按一次退出程序", Toast.LENGTH_SHORT).show(); }else{ isToFinish = true; } return isToFinish; }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ if(ComUtils.finishTwoBack(this)){ return super.onKeyDown(keyCode, event); }; return true; } return super.onKeyDown(keyCode, event); }
时间: 2024-10-13 15:28:57