In Android: how to call an activity function from a service?

I have an Activity (A) and a Service (S) that starts with A as follows:

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

A have the same function in A:

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

Now I want to call A.someInfoArrived (i) from S. Intent.putExtra does not have a version where I could pass a link to an object, etc. .... Please help!

PS: Another way (poll S for new information) is NOT what I need. I found enough information on how to do this.

+7
android callback android-intent android-activity service
source share
1 answer

One option is to switch from startService() to bindService() and use a local binding pattern. Then you need to have a call method on S at some point to register a listening or callback method. Then S will have something that he can call A when some information arrives. You can see an example project for here .

Alternatively, you can leave startService() in place and use the Intent broadcast to let S inform A of any information. You can see an example project demonstrating such an Intent translation here .

+14
source share

All Articles