0

Notify activity from service

I want to know if it is possible to do what the selected answer in the above post said, when your activity and service are in separate packages. Basically i have an object that is non-serializable (lets say a created view) and I want to send it from my service to my activity. Would be easy enough by using a custom binder, but as i've found out, you cant use custom binders when your service and activity are in separate packages.

I've been pondering this for a few weeks and it has really put a block in my project I am working on.

For those who will ask, I am trying to make a framework that allows "plugins" from other packages. But I am unsure how one would send non-serializable date back and forth between said service and activity.

Community
  • 1
  • 1
miversen33
  • 573
  • 5
  • 19

1 Answers1

0

It depends on the complexity of the object, If the object that you want to serialize is an object that comes from the Android SDK lets say a RelativeLayout or a Cursor I don't see that happening anyway, because those objects contains references to another objects that you don't have access to modify or make them implement the Serializable interface.

If your object is a class that you implemented and all references inside that class are also to another classes that you implementad (or to Serializable/Parceable objects) then sure you can. One way of doing so is, well, making them implement Serializable or making your own Parceable theres plenty of tools to achive this in a quick way like this or this one.

If no one of this answers satisfies you, then tell me what're you trying to send from the Service.

EDIT

Did you try to make a class implement both Serializable and OnClickListener and send it through the intent?

Sounds like you need some sort of Command pattern there.

4gus71n
  • 3,717
  • 3
  • 39
  • 66
  • The idea is a RelativeLayout yes, though it is not necessary. I have more so noticed that I can pass what is needed and have the activity create the relativelayout and put in the info needed. What I cant do though is handle touches. My activity has no idea what to do with views when they are touched because I have no onClickListener attached to the views. Thus I am trying to send an OnClickListener object from the service to the activity, but to no avail. – miversen33 Oct 17 '15 at 22:36
  • I edited my answer but I recommend you taking the second approach, don't rely on the `OnClickListener` instead try to make your own commander object, send it through the bundle of the intent and instantiate the `OnClickListener` in the application as you do with the layout – 4gus71n Oct 17 '15 at 22:49
  • That is an interesting idea. So make my framework have some sort of static "Command" class and let it pass back the command based on the click and then the service and do whatever it needs to do with that? – miversen33 Oct 17 '15 at 22:51
  • Sorry I've to go, later I'll give you a concrete example check the command pattern, basically the command will tell your app what the onclick has to do – 4gus71n Oct 17 '15 at 22:53
  • I'll tinker with the idea as I believe this is what I need, and I am curious to see what you have for an example. – miversen33 Oct 17 '15 at 23:43