Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Can’t cancel a notification after updating it

Posted on November 14, 2022

Solution 1 :

First create a Timer like this

private Timer timer;
    private TimerTask timerTask;
    public void startTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            public void run() {
                // Add your code
            }
        };
        timer.schedule(timerTask, 1000, 1000); //
    }

Also you need to stop your timer.
So

 public void stoptimertask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

Call StartTimer and StopTimer in OnStartCommand() and onDestroy() respectively. Add these lines in onDestroy()

Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        broadcastIntent.setClass(this, Restarter.class);
        this.sendBroadcast(broadcastIntent);

Solution 2 :

it can be handled in multiple ways, you have not stopped your timer

Note:- posting code in Kotlin

1)

override fun onDestroy() {
   counter.cancel()
}
  1. in your activity
override fun onResume() {
    super.onResume()
    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
     notificationManager.cancelAll()
}

Problem :

I’m developing a countdown app, and currently trying to show a notification when you exit the app while the countdown is running. Correspondingly, I want the notification to disappear when the user returns to the app.

So far I’ve managed to make it work for a simple notification with static text, do the following: in MainActivity.java, in onStop(), I create an intent and initiate the service with startService(intent). Symmetrically, in onStart() I run stopService(intent) so that when you return to the app the service gets canceled. This works like a charm, the notification appears and disappears when it must.

The next step has been trying to make the notification show a text that varies (it will say “X minutes remaining”). According to the info out there, to update an existing notification you have to create a new one, give it the same ID as the existing one, and call .notify of a NotificationManager. When I do this the notification indeed gets updated correctly (the text changes as expected), BUT: now, returning to the main activity does not cancel the notification. The icon stays up there and doesn’t get interrupted.

READ  [ANSWERED] android - Measuring children in custom view breaks in RTL
Powered by Inline Related Posts

I’ve been trying to solve this for hours and hours. I’ve also tried hacks like sending signals via shared preferences to tell the service to stop, but for some reason, it seems to completely ignore the command stopself() too.

Does anybody have a suggestion of what could be the cause? Any help would be greatly appreciated. Here is the relevant code:

MainActivity.java:

    @Override
protected void onStart() {
    super.onStart();

    Intent serviceIntent = new Intent(getBaseContext(), CounterService.class);
    stopService(serviceIntent);

}

protected void onStop() {
    super.onStop();
    Intent serviceIntent = new Intent(getBaseContext(), CounterService.class);
    startService(serviceIntent);

}

CounterService.java:

public class CounterService extends Service {
Notification notification;
NotificationManager notificator;
Intent intentNoti;
CountDownTimer counter;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

intentNoti = new Intent(this, MainActivity.class);
final PendingIntent pending = PendingIntent.getActivity(this, 0, intentNoti, 0);

final Bitmap icon = BitmapFactory.decodeResource(getResources(),
            R.drawable.common_full_open_on_phone);

//Countdown
counter = new CountDownTimer (30000, 1000) {

        public void onTick(long millisUntilFinished) {
           String time = String.valueOf(System.currentTimeMillis());

            notification = new Notification.Builder(CounterService.this)
                    .setContentTitle("Name")
                    .setContentText(time)
                    .setSmallIcon(R.drawable.icon_start)
                    .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                    .setContentIntent(pending)
                    .setOngoing(true).build();

            notificator = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificator.notify(1001, notification);


        }

        public void onFinish() {
        }

    }.start();
    return START_STICKY;
  }
 @Override
    public void onDestroy() {
        super.onDestroy();
        counter.cancel();
    }
}

Comments

Comment posted by Abderian

Thank you for your reply… I’m going with Rohan Pawar’s solution instead because it is a one liner, my app only has to manage one notification and it does the trick… But thank you for your code.

Comment posted by Abderian

Thank you! This did the trick… You’re right about the clock, I’ve modified my post to add the onDestroy() method… Thank you so much, you saved me…

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress