What's the best way to make a request from an android service to an activity or service and wait for a response?

StackOverflow https://stackoverflow.com/questions/21842119

  •  13-10-2022
  •  | 
  •  

Question

I've looked at all the IPC mechanisms in android and none seem to do what quite I'm looking for, perhaps there's a better way...

I have a service that's starting up, and it needs to get information from another service or activity in an entirely different 2nd application.

startActivityForResult is what I'm looking for, but that's not available from a service, only an activity. I'd really prefer to get the information from the 2nd app's activity, but I'm happy to get it from the 2nd app's service.

But the part I'm really interested in, is making the synchronous call from the service and have it wait for a response. I can send a broadcast intent from the service to the 2nd app's service, but then there's no tie back when the 2nd app's service sends a broadcast intent back with the answer I want.

Is there any way to do something like this from a service?

Was it helpful?

Solution

Is there any way to do something like this from a service?

The calls made to a Binder from a bound service are synchronous. The documentation discusses bound services, with a separate page on the AIDL you will need since the services are in different applications.

Personally, I would rewrite the first service to not need the synchronous call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top