Search code examples
javaandroidcountdown

Notification closed and reopened after using cancel();


I have a problem with my app. I have 2 button Start and Stop, when I click start notification is created and updated in every tick of countdown timer, but I want to close notification when Stop button is pressed. I used notificationmanager.cancel(id) but notification is closed and re opened again. What wrong I did? Here is my code:

private void startButtonClick() {
    try {
        time = Integer.parseInt(timeInMinutes.getText().toString());
        if (time == 0) {
            throw new NumberFormatException("Time can't be lower than 1");
        }
        startButton.setVisibility(View.INVISIBLE);
        pauseButton.setVisibility(View.VISIBLE);
        timeInMinutes.setVisibility(View.INVISIBLE);
        countDownTextView.setVisibility(View.VISIBLE);
        final NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("Timer")
                        .setContentText("12312312");
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        new CountDownTimer(time * 60000, 500) {
            public void onTick(long millisUntilFinished) {
                if (isCanceled) {
                    cancel();
                    buttonsVisibility();
                    isCanceled = false;
                }
                int minutes = (int) (millisUntilFinished / 1000) / 60;
                int seconds = (int) (millisUntilFinished / 1000) % 60;
                countDownTextView.setText("Time left: " + String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
                mBuilder.setContentText("Time left: " + String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
                notificationManager.notify(3, mBuilder.build());
            }

            public void onFinish() {
                turnOffBluetooth();
                turnOffWiFi();
                turnOffAudio();
                turnOnSilentMode();
                buttonsVisibility();
                notificationManager.cancel(3);
            }
        }.start();
    }
    catch (NumberFormatException e){
        Toast.makeText(getApplicationContext(), "Enter time. Only numbers!", Toast.LENGTH_SHORT).show();
    }
}
private void pauseButtonClick() {
    Toast.makeText(getApplicationContext(), "Timer off", Toast.LENGTH_LONG).show();
    startButton.setVisibility(View.VISIBLE);
    pauseButton.setVisibility(View.INVISIBLE);
    isCanceled = true;
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(3);
}

Solution

  • Even though you have canceled the timer you haven't finished the method so it will continue execution to avoid that you can try this

    if (isCanceled) {
                        cancel();
                        isCanceled = false;
                    } else {
                        int minutes = (int) (millisUntilFinished / 1000) / 60;
                        int seconds = (int) (millisUntilFinished / 1000) % 60;
                        mBuilder.setContentText(
                            "Time left: " + String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
                        notificationManager.notify(3, mBuilder.build());
                    }