Android: Passing variables to an already running service

12,716

If your service is not an IntentService, you can call startService(...) as many times you want. The service will run the first time but next calls will result in new onStartCommand() calls with the new extras you need.

Check this answer and the doc.

Share:
12,716

Related videos on Youtube

Daniel Flannery
Author by

Daniel Flannery

Software Engineer. Polyglot programmer.

Updated on June 17, 2022

Comments

  • Daniel Flannery
    Daniel Flannery almost 2 years

    I am having issues passing a value from an Activity to an already running service. I was wondering what the best approach to take would be? Adding extras wont work as I believe this has to be done before the intent is started? (correct me if i'm wrong).

    Any help would be great! I can elaborate if needed.

    Dan.

  • Sam
    Sam about 11 years
    Agreed, here's more proof from Activity#startService()'s documentation.
  • Daniel Flannery
    Daniel Flannery about 11 years
    Ah I see. Dont know how I missed this reading the documentation. Thanks for your swift answer!
  • scai
    scai over 7 years
    Also see this answer which explains how to to call startService() to pass new Intents and read them inside your service during onStartCommand(). Essentially the same answer as here but a little more verbose.