0

Hi I am working in chat app. I have timer and when customer gives request to expert, the timer starts to run for 1 minute and waits till it get approve. After approved/reject, timer will get cancelled. this works in normal scenario. But when app gets minimised by giving request, timer is not getting cancel even if expert approved or rejected. How to handle this situation?

My code is as follows:

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);
Shadow
  • 6,864
  • 6
  • 44
  • 93
  • What does your onPause do? – TejjD Jan 05 '16 at 12:04
  • 1
    When your activity goes background, it can be destroyed by os (so its TimerTask finishes). If you put the code above in a service, it can run after the activity gets destroyed too. – nvi9 Jan 05 '16 at 12:16

0 Answers0