7

I have an Activity (A) and a Service (S) which gets started by A like this:

Intent i = new Intent();
i.putExtra("updateInterval", 10);
i.setClassName("com.blah", "com.blah.S");
startService(i);

A have a function like this one in A:

public void someInfoArrived(Info i){...}

Now I want to call A.someInfoArrived(i) from within S. Intent.putExtra has no version where I could pass an Object reference etc ... Please help!

PS: The other way around (A polling S for new info) is NOT what I need. I found enough info about how to do that.

nex
  • 685
  • 1
  • 9
  • 21

1 Answers1

14

One option is to switch from startService() to bindService() and use the local binding pattern. Then, you need to have A call a method on S at some point to register a listener or callback method. Then, S will have something it can call on A when some info arrives. You can see a sample project for that here.

Alternatively, you could leave startService() in place and use a broadcast Intent to let S tell A about some info arriving. You can see a sample project demonstrating such a broadcast Intent here.

Guido
  • 46,642
  • 28
  • 120
  • 174
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thank you, that is exactly what I was looking for! My hopes and believes in forums have been restored :) – nex May 16 '10 at 14:31
  • Just implemented your second suggestion and it works like a charm. thanks again! – nex May 16 '10 at 15:03