- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // 两次返回退出程序
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if ((System.currentTimeMillis() - mExitTime) > 2000) {
- Toast.makeText(this, R.string.toast_twice_press_back_to_exit,
- Toast.LENGTH_SHORT).show();
- mExitTime = System.currentTimeMillis();
- } else {
- finish();
- }
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
函数放置在自己的Activity里面,记得定义mExitTime变量及初始化,已验证,效果不错!
时间: 2024-10-09 11:06:46