按两次返回键退出程序 博客分类: android 退出两次返回键
程序员文章站
2024-03-24 16:53:16
...
在Activity中重写onKeyDown方法即可
private long mPreKeyBackTime = 0; private static final int EXIT_KEYBACK_DELTA_TIME = 2000; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if ((System.currentTimeMillis() - mPreKeyBackTime) > EXIT_KEYBACK_DELTA_TIME) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); mPreKeyBackTime = System.currentTimeMillis(); } else { finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); }