Android 返回键退出APP

    private int ExitTime = 0;
   
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(ExitTime == 0)
            {
                ExitTime = System.currentTimeMillis();
                showToastOver("再按一次退出APP");
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        ExitTime = 0;
                    }
                },2000);
            }else{
                if(System.currentTimeMillis() - ExitTime > 300)
                {
                    ExitTime = 0;
                    System.exit(0);
                }
            }
        }else{
            super.onKeyDown(keyCode,event);
        }

        return true;
    }

实现再按一次退出APP



你可能感兴趣的:(android)