In my Android app I have a service that is started via AlarmManager. I want to have an activity that will display some progress/status info for this service. Since I already have 90% of the code done, and the service is working fine as-is, I’m wondering what is the easiest way (if possible) to just have the service report progress info to the activity, only if the activity is running.
I saw “binding” but I think that might only work if I want to start the service from my activity and bind to it. Possibly a broadcast receiver?
I would like to be able to report status updates fairly often, maybe even several per second in some cases, so I don’t want to do something like use PreferenceManager.
Does my service and activity run in the same application scope? If so, maybe I can create a singleton static object to share status info? The status info can just be a string, that is all I really need.
You can use a Broadcast to “emit” data from your Service. Then in your Activity have a
BroadcastReceiverfor this same event. When no Activities are running that respond to that broadcast then nothing will receive them.