Android Service interacting with multiple activities

17,556

Solution 1

I usually bind my service from the Application class and have some kind of controller class (like a "mediator" I guess...not sure how all these patterns are named) scoped in the application that handles communications between services and whatever the active Activity is.

This would involve writing your own Application class and telling the Manifest to use this one. I went into more detail on this process in a previous thread:

More efficient way of updating UI from Service than intents?

You could keep track of the "currently active" Activity by sending the Application class a reference to itself in onResume (also explained in the example above). This can be accomplished by deriving your Activities from a common base class that has a way of getting your Application class (casting from getApplicationContext), and in this base class' onResume, send a ref of itself to the application. Then, you can register activities by name with your DataServiceController, for example, and send messages to the current Activity only if it's registered with the Controller to receive them.

Solution 2

Definitely more than one activity can bind to your service. You will get an onBind() for each one that binds. Your service would then ideally handle the logic of interacting with multiple activities by identifying them using an ID or the intent (with your own IDs for each activities as extras) from onBind() in your service. You could then have the Service spawn off a background thread for each activity that binded to it.

Share:
17,556
Soumya Simanta
Author by

Soumya Simanta

Updated on July 20, 2022

Comments

  • Soumya Simanta
    Soumya Simanta almost 2 years

    I'm trying to refactor/redesign an Android app. Currently, I've one UI activity (Activity 1) that creates a DataThread. This thread is responsible for network I/O and interacts (provides data) with the UI activity via a handler.

    Now, I want to add another activity (a new UI screen with Video) - Activity 2. Activity 1 is still the main activity. Activity 2 will be invoked when the user clicks a button on Activity 1. Activity 2's data also comes from the DataThread.

    My idea is to put the logic of my DataThread inside an Android Service (DataService). My question is - can more than on activity bind to my DataService at the same time? Is there a way to tell the service to provide data to a specific activity only?

    Any other ideas are welcome?

    Thanks in advance.

  • Sam
    Sam about 13 years
    i'm looking for solution like this,is there any working example or link to get more details about this implementation, thx
  • Sven Jacobs
    Sven Jacobs over 12 years
    @Rich How do you manage to cleanly unbind from the Service when your application terminates if you have bound it in your Application class? Because Application#onTerminate() is never called on production devices, see JavaDoc developer.android.com/reference/android/app/Application.html
  • OneWorld
    OneWorld almost 11 years
    @SvenJacobs The only way I can think of: Let the Service call stopSelf() after some time when all activities are paused. Activities have to tell the service their state in this case when they are getting paused. Activities never call unbindService() and the Service gets bound to the applicationContext on each onResume() of each Activity.
  • Joakim Engstrom
    Joakim Engstrom about 10 years
    There is a big caveat doing this which @SvenJacobs pointed out. If one do as Rich explains, then one would really have to careful to 1. Not have long running threads outside application (battery issues for one) 2. Careful not to leak the activity reference which could cause several issues.
  • Stephen Tetreault
    Stephen Tetreault over 9 years
    @JoakimEngstrom so why didn't you edit it so it is more clear?