Android连续点击两次返回键退出程序的三种实现方法

一 主流的方法,重写onKeyDown(int keyCode, KeyEvent event),当按键为KeyEvent.KEYCODE_BACK(返回键)时,判断isExit的值,当为首次点击时,执行自定义方法,第二次点击执行父类方法,退出程序.


                private boolean isExit;

                                @Override
                                public boolean onKeyDown(int keyCode, KeyEvent event) {

                                                if (keyCode == KeyEvent.KEYCODE_BACK) {
                                                                handle();
                                                                return false;
                                                }else{
                                                                return super.onKeyDown(keyCode, event);
                                                }

                                }

                                private void handle() {
                               
                                                Timer timer = new Timer();

                                                if (!isExit) {
                                                                isExit = true;
                                                                //第二次点击,用到了TimerTask
                                                                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                                                                timer.schedule(new TimerTask() {
                                                                                @Override
                                                                                public void run() {
                                                                                                isExit = false;
                                                                                }
                                                                }, 2000);
                                                } else {
                                                                Log.i(TAG, "exit()");
                                                                finish();
                                                                System.exit(0);
                                                }

                                }

二 第二种要稍微巧妙一点,代码也精简一点,直接重写onBackPressed(),在方法内部实现全部功能.

                @Override
                public void onBackPressed() {
                                if (isExit) {
                                                super.onBackPressed();
                                                return;
                                }

                                this.isExit = true;
                                Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

                                new Handler().postDelayed(new Runnable() {

                                                @Override
                                                public void run() {
                                                                isExit=false;
                                                }
                                }, 2000);
                }

三 重写onBackPressed(),利用系统时间巧妙判断

        @Override
    public void onBackPressed()
    {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
        {
            super.onBackPressed();
            return;
        }
        else {
            Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show();
        }

        mBackPressed = System.currentTimeMillis();
    }

你可能感兴趣的:(Android连续点击两次返回键退出程序的三种实现方法)