安卓双击退出程序 --- 返回上一界面

直接上代码
重写 onKeyDown方法 中间的if是我自己加的判断 不需要删掉即可
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (MyApplication.getUserInfo() != null && MyApplication.getUserInfo().getExtend() != null && MyApplication.getUserInfo().getExtend().getUser() != null) {
exit();//退出
}else {
onBackPressed();//返回上一界面
}
return false;
}
return super.onKeyDown(keyCode, event);
}

public void exit() {
    if ((System.currentTimeMillis() - exitTime) > 2000) {
        Toast.makeText(getApplicationContext(), "再按一次退出程序",
                Toast.LENGTH_SHORT).show();
        exitTime = System.currentTimeMillis();
    } else {
        finish();
        System.exit(0);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43148719/article/details/89711002