0

I have made this app that offers it's functionality almost completely in a notification. The configuration is done in the app itself. This is the structure I have now:

  • Service 1: retrieve data and changes to that data from FireBase and save it in an object. Everything the app needs to show in the app itself is in this object. It gets distributed to other activities via the observable pattern. So these activities implement Observer.

  • Service 2: this service keeps track of external changes that can directly affect the object in service 1. Therefore, this service is bound to service 1.

  • Activities: some activities to display the data. All these activities implement Observer and observe the object that's stored in Service 1.

So far so good. The above gives me no problems whatsoever. I can communicate with both services from every activity. Every activity gets an update if the object they are interested in changes and figures out what to do with the updated data.

Now I have this notification, all of it's actions are broadcasted intents, for which I have a BroadcastReceiver that handles these intents. The broadcastreceiver may need data from Service 1 to execute it's action, and all actions are located in simple classes (no activity or service), but I can't bind a broadcastreceiver or objects to a service.

As I see it I have 2 possible solutions here:

  1. Create another service in which all possible "notification" actions are located. This service can be bound to Service 1 and thus can retrieve all the data it needs.
  2. Make the observable object in Service 1 static, so even the objects in which the "notification" actions are located can access it.

Possible problems for solution 1: maybe 3 services is a little over-kill? But putting all functionality in the same service would create a big, unclear service.

Possible problems for solution 2: I've read that static variables are not best practice, because static variables represent global state and are hard to reason about. Source

I hope someone can tell me if I'm right on the above assumptions, and if there's a solution I have not mentioned here.

JRG
  • 4,037
  • 3
  • 23
  • 34
Beuz
  • 193
  • 1
  • 12
  • instead of `PendingIntent#getBroadcast` use `PendingIntent#getService` when making your `PendingIntent` - this `PI` should point directly to your service 1 – pskink Aug 02 '17 at 07:35
  • Thanks for your quick response. But your solution also indicates that all my "notification" actions, that need the data from service 1, should be located in a service also. So what you'r saying is replace my BroadcastReceiver with a service, and let every pending intent refer to that service? Edit: or do you mean i should replace my BroadcastReceiver by an IntentService? – Beuz Aug 02 '17 at 08:01
  • i mean that broadcast is redundant here: all actions you want to do after clicking your notification can be done in your service: just override `Service#onStartCommand` and handle your actions here - this is like your solution #1 but instead of adding 3rd service that binds to service 1 you do all your stuff in service 1 – pskink Aug 02 '17 at 08:06
  • Hmm, I get what you mean, but it would make the service big and unclear (800 lines of code). What is the down-side of having multiple services? – Beuz Aug 02 '17 at 08:24
  • you have to talk to them somehow... if you want your service short then add some external handler classes where your messages will be handled - so your service simply dispatches messages to their handlers – pskink Aug 02 '17 at 08:30
  • Thanks! I agree with you, I need some refactoring to build it up this way, but it seems the best/only way to do it. Thanks again, this certainly helps a lot. – Beuz Aug 02 '17 at 08:34
  • sure, good luck! – pskink Aug 02 '17 at 08:36

0 Answers0