3

if I start a service with startForeground(myID, not), can I replace the not for another one without stopping the foreground process? I mean, I have a service that will do two task in a row before service ends, and I want to notify user when the first ends without the need to stop foreground Service?

Also, is it possible to start a foreground service with no notification (i.e. NULL).

any stretegy I can use instead of? thanks.

Tibor
  • 589
  • 2
  • 7
  • 20

2 Answers2

1

The general answer to your question is to use the NotificationManager.notify() method using the same id passed into the startForground() method when starting the service.

Also worth noting :

  • Updating the notification will NOT remove the service's foreground status.
  • You cannot cancel the notification using the NotificationManager.notify. Instead you must call stopForeground() which removes the service's foreground status as well.

Refer to the answer given by Luca and the discussion under CommonsWare's answer, as well as the answers given in by Lorne Laliberte and dmmh.

How do I update the notification text for a foreground service in Android?

Does updating a Notification remove a Service's foreground status?

Community
  • 1
  • 1
TheIT
  • 11,919
  • 4
  • 64
  • 56
-2

In NotificationManager there is method cancel(int) that receive notificationId, so you can just cancel old notifier and create a new one.

Jin35
  • 8,602
  • 3
  • 32
  • 52
  • but the question is if I cancel ID that what passed to startForeground(id,not), does it will return service from foreground to a normal service.... – Tibor Dec 15 '11 at 14:22
  • I'm not sure but I dont think that `NotificationManager.cancel()` can somehow influence on service – Jin35 Dec 15 '11 at 17:19
  • @Tibor, what you found out? Does cancel imply on turning the Service back to background? This answer [here](http://stackoverflow.com/a/8423777/914874) says it does. – Eduardo Apr 02 '12 at 10:47
  • 1
    This was a lot of time ago, but what I remember is I used startForeground and it replaced the notification, no need to cancel the previous one. – Tibor Apr 02 '12 at 16:05