nhaarman
nhaarman

Reputation: 100378

Activity missing broadcast from service

I have a custom Chronometer which extends TextView and works as a stopwatch. The Activity that uses this Chronometer is listening for broadcasts from a Service. In this broadcast the Service tells the Activity (and thus the Chronometer) to stop counting. This works fine when keeping the application on foreground.

However, when the Activity is not active (like the user presses the home button), the Activity doesn't get the broadcast, and the Chronometer keeps counting. So when I open the Activity again, the Chronometer is still counting up, when it shouldn't be.

Who can provide me with a solution for this?

Note: the Service does not have any knowledge of the Activity nor the Chronometer. It just sends a broadcast "Hey, I'm done with whatever I was doing!".

Upvotes: 0

Views: 244

Answers (1)

nullpotent
nullpotent

Reputation: 9260

Your service should be called with startForeground(int id, Notification notification);

See why...

By the way, your BroadcastReceiver shouldn't be unregistered within your activity. Add it to the manifest with a intent filter COUNTDOWN_STOP or whatever, to achieve this.

Upvotes: 1

Related Questions