0

I have a alarm that triggers a countdowntimer with a notificaton that counts down and updates the notification.

The problem is that when the notificaton is dissmised than the timer creates a new one.

How can I stop the timer when dismissing the notification?

My code below:

public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {



    String time = intent.getStringExtra("Time");
    long milliSeconds = GetTimeToNextPrayerInMilliseconds(time);

    int intentId = intent.getIntExtra("IntentId", 0);

    CreateCountDownTimer(milliSeconds, intentId);

}

private void CreateCountDownTimer(long milisecondsToFinish, final int intentId) {
    CountDownTimer timer = new CountDownTimer(milisecondsToFinish, 1000) {

        @Override
        public void onTick(long millis) {
            //int seconds = (int) (millis / 1000) % 60;
            int minutes = (int) ((millis / (1000 * 60)) % 60);
            int hours = (int) ((millis / (1000 * 60 * 60)) % 24);
            String text = String.format("%02d:%02d", hours, minutes);

            Notification notification = new Notification.Builder(_context)
                    .setContentTitle("Title")
                    .setContentText("USA")
                    .setSmallIcon(R.drawable.logo)
                    .build();

            NotificationManager notificationManager = (NotificationManager) _context.getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(intentId, notification);

        }

        @Override
        public void onFinish() {

            NotificationManager notificationManager = (NotificationManager) _context.getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(intentId);

        }
    }.start();
}
mrjasmin
  • 1,230
  • 6
  • 21
  • 37

1 Answers1

0

You want to create a DeleteIntent and catch that to cancel your timer.

See catch on swipe to dismiss event for how to implement a DeleteIntent.

CodeChimp
  • 4,745
  • 6
  • 45
  • 61