Last Updated: February 25, 2016
·
2.261K
· kusand

Ongoing Notifications Within An IntentService

Recently, I encountered a strange bug. I was working on an IntentService responsible for notifying the user upon receiving an intent; among other things it could do was update a notification about something in progress. It looked fine in most cases, but I discovered that if it was being updated while a "Looking For GPS" or keyboard selector notification was also up, it would 'flash' on each update - disappear and reappear redrawn.

I have yet to delve into the Android code to understand the disparity, but the solution was straightforward -the IntentService was building an entirely new Notification each time instead of re-using the same builder/notification to generate it.

The takeaway: if you plan to have an ongoing notification, it seems you'll need to hold on to that notification/builder object so you can notify the NotificationManager with it in order to prevent flashing.

1 Response
Add your response

Hi :)

How did you actually solve that in code. I just can't find a way to keep the Builder object between several invocations of the IntentService.

Edit: Solved it by now. You preserve the Builder simply in a static field. You lazily initialize that field on the first access. You use getApplicationContext() to get the global application context to initialize the Builder. That context will be available even when the Service is killed.

Thanks and Cheers
Patrick

over 1 year ago ·