views:

104

answers:

2

I have an Android Service that I would like to keep running even after the last Activity has been popped off the stack, or the User has chosen to do something else.

Essentially the Service is listening for changes on a remote server, and I would like to generate a Notification if and only if an Activity from the app isn't running(or visible). In other words, I don't want the Notifications to occur while the User is directly interacting with the app.

In the case where the User is directly interacting with the app, the Service will notify the Activity and update appropriate UI elements based on the changes. I plan to implement this through the Observer pattern.

How can the Service know if none of apps Activities are bound to it?

Thanks, J

A: 

Have you tried onBind() and onUnBind()?

http://d.android.com/reference/android/app/Service.html

Or more simply have a static int inside your service that is incremented/decremented every time an Activity starts/stops. When the count == 0 then you are ok to launch your notification.

disretrospect
A: 

How can the Service know if none of apps Activities are bound to it?

You know if there are no bound Activities if your Observer list is empty.

CommonsWare