Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle IPC between a service and an activity (and its subactivity)?

Communication between two different processes (a service and an activity) in Android can be managed via Messenger or AIDL: it is sufficient that an activity binds to a service.

However, what happens if this activity has one or more sub-activity? From the moment when the main activity starts a sub-activity, I would like the communication to be redirected to the sub-activity; similarly, when the sub-activity is destroyed, I would like the communication is redirected back to the main activity, etc..

Example # 1:

  1. MyService <---IPC---> MainActivity
  2. MainActivity launches SubActivity, then MyService <---IPC---> SubActivity
  3. SubActivity is destroyed, then MyService <---IPC---> MainActivity

Example # 2:

  1. MyService <---IPC---> MainActivity
  2. MainActivity launches FirstSubActivity, then MyService <---IPC---> FirstSubActivity
  3. FirstSubActivity launches SecondSubActivity, then MyService <---IPC---> SecondSubActivity
  4. SecondSubActivity is destroyed, then MyService <---IPC---> FirstSubActivity
  5. FirstSubActivity is destroyed, then MyService <---IPC---> MainActivity

How to handle these cases?

like image 404
enzom83 Avatar asked May 07 '12 16:05

enzom83


People also ask

How do you communicate between service and activity?

This example demonstrates how do I communicate between Activity and Service in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I send a callback from service to activity?

You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause() ) and notify it via a broadcast, providing an Intent .

How to use Intent to start another activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How do you notify activity of a service?

Show activity on this post. Use a Handler in your service that registers a client when your ListActivity connects to the service; that is, in your onServiceConnected method in your ListActivity , send a Message to your service that enables you to keep track of connected clients.


2 Answers

You want to have a single entity that is responsible for binding to the service and holding on to the connection and you need that entity NOT to be an Activity instance. Try this:

  • Create a base class (BaseActivity) that subclasses Activity
  • Derive all your activities from BaseActivity
  • Manage the connection between your application and your service using methods in BaseActivity. BaseActivity will need to have static (class) variables that keep track of the connection to the service and deal with binding to the service and shutting it down when you are done with it.
  • Make sure to use the application context (not the activity context) when binding to the service so that the OS won't kill the connection to the service when the activity is destroyed.

In this way you don't have to worry about creating and tearing down connections between the different activities and your service. There is only ever one connection between your entire application (all the activities) and your service.

I realize that I haven't explained all the gory details, but hopefully you get the basic idea.

like image 101
David Wasser Avatar answered Sep 30 '22 10:09

David Wasser


Have you ever think about the following solution? Instead of bind Activity to Service, you can start your activity with the command startService() and then communicate with Intents and Receivers. In this way you can launch other activity, calling service for its state and interact with it wherever you want :)

like image 23
Simone Casagranda Avatar answered Oct 02 '22 10:10

Simone Casagranda