0

嗨,我在聊天应用程序中工作。我有计时器,当客户向专家提出请求时,计时器开始运行 1 分钟并等待它获得批准。批准/拒绝后,计时器将被取消。这在正常情况下有效。但是,当应用程序通过发出请求而最小化时,即使专家批准或拒绝,计时器也不会取消。如何处理这种情况?

我的代码如下:

private static final int SECONDS_DELAY = 1000;

//when button clicked, request been sent and wait for approve..so timer s   tarts to run for a minute..
 case R.id.btn_alrt_ok:
        sendRequestToExtend();
        break;

//here i am performing logic..
private void sendRequestToExtend() {
    try {
        showRequestTimer(); //displaying timer to run..
        isFirstReq = false;
        sendChatCommand(Constants.CODE_CHAT_EXTEND + ":" + selectedOption);
        elapsed = 0;
        chatExtendTimer = new TimerTask() {
            @Override
            public void run() {
                elapsed += SECONDS_DELAY;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateReqTimer(elapsed / SECONDS_DELAY,
                                Constants.SECONDS_15);
                        if (elapsed >= Constants.CHAT_RESP_TIME)
                            //when app gets minimised this method is not working..
                            onChatInvitation(Constants.CHAT_EXT_REJECT,
                                    chatToPerson);
                           //in this method onChatInvitation, for request if approved in expert, i am making timer dialog to dismiss. so it's working fine if app not minimised..
                    }
                });
                return;
            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(chatExtendTimer, SECONDS_DELAY,
                SECONDS_DELAY);
4

0 回答 0