Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Service and Activity interaction

I want to create an app that contains a Service S and an Activity A. The Service S is responsible for preprocessing, such as preparing the data shown on the UI of the Activity A, before the Activity A gets invoked.

I want to be able to invoke the Service S from outside the package, say from another Android app's Activity class B, do the preprocessing, and then when the data is ready, invoke Activity A.

My questions are:

  1. What is the best way to share data between the Service S and Activity A?
  2. How can the external activity B communicate with the Service S to determine if it has completed with all its preprocessing, and the Activity A is ready to be invoked?

Thanks Chris

like image 922
Chris Avatar asked Jun 17 '10 21:06

Chris


1 Answers

What is the best way to share data between the Service S and Activity A?

Use the local binding pattern and have Activity A bind to Service S, then call the service's exposed API to retrieve whatever is needed.

How can the external activity B communicate with the Service S to determine if it has completed with all its preprocessing, and the Activity A is ready to be invoked?

Use the remote binding pattern and AIDL. Activity B would register an AIDL-defined callback with Service S, which the service would invoke when appropriate. See here and here for an example.

like image 133
CommonsWare Avatar answered Oct 27 '22 00:10

CommonsWare