2019年10月5日星期六

Android Studio 双击后退按钮退出程序

为了双击后退按钮退出安卓程序, 在MainActivity加入下列代码。
    long backKeyPressedTime;
    :
    :
    @Override //2秒内双击则退出程序
    public void onBackPressed() {
        //第一回按退出程序
        if(System.currentTimeMillis()>backKeyPressedTime+2000){
            backKeyPressedTime = System.currentTimeMillis();
        }
        //第二回按退出程序
        else{
            finish();
        }
    }

没有评论:

发表评论