120

Usually when I create an Android service I implement the onCreate method, but in my last project this does not work. I tried implementing onStartCommand, and this seems to work.

The question is: when I have to implement a service which method is required? Which methods I have to implement? onCreate, onStartCommand, or both? And what is the role of each?

1
  • 1
    onCreate is always called regardless Jun 5, 2020 at 17:13

2 Answers 2

211

onCreate() is called when the Service object is instantiated (ie: when the service is created). You should do things in this method that you need to do only once (ie: initialize some variables, etc.). onCreate() will only ever be called once per instantiated object.

You only need to implement onCreate() if you actually want/need to initialize something only once.

onStartCommand() is called every time a client starts the service using startService(Intent intent). This means that onStartCommand() can get called multiple times. You should do the things in this method that are needed each time a client requests something from your service. This depends a lot on what your service does and how it communicates with the clients (and vice-versa).

If you don't implement onStartCommand() then you won't be able to get any information from the Intent that the client passes to onStartCommand() and your service might not be able to do any useful work.

17
  • 7
    Can two instances of the same Service exist within an application?
    – jacktrades
    May 30, 2013 at 11:44
  • 1
    @jacktrades Theoretically and according to the documentation: No. However, there seems to be some bug in Android where indeed in certain circumstances it will create multiple instances of a Service. However, as I said, this is a bug, and we have not been able to reliably reproduce it at will. Why are you asking? May 30, 2013 at 21:01
  • 3
    @Mr_and_Mrs_D There are no official bugs reported as far as I know. However, there are several reports of this happening and I have an application where I know that it is happening. I have been unable to reduce the problem to something small and repeatable so I have not filed a bug myself. The documentation does not explicitly state that services are singletons, but it is very clear that that is what is intended. Look at the docs for 'startService()`: clearly states that if the service is not already running, it will be created and started, if it is running it remains running Nov 18, 2013 at 22:56
  • 8
    @Diffy When you call startService(), if the service is not running, Android will create an instance of the service class (this is a service object) and will then call onCreate() on that object. It will then call onStartCommand() on that object. If, some time later, you again call startService(), if the service is still running, Android will not create a new service object. Instead, it will just call onStartCommand() on the existing service object. Is that clear? Oct 25, 2014 at 22:06
  • 1
    So after knowing this, where should I call startForeground()? On onCreate() or on onStartCommand()? (And why?) Still, thank you very much for the explanation!
    – Edu590
    May 7, 2021 at 18:40
14

Service behave same like Activity Whatever you want to associate once with a service will go in onCreate like initialization

and whenever the service is called using startService. onStartCommand will be called. and you can pass any action to perform . like for a music player , You can play ,pause,stop using action

And you do any operation in service by sending an action and receiving it on onStartCommand

onCreate work like a Constructor.

Edit in Short

onCreate() calls only for the first time you start a Service Whereas onStartCommand() calls everytime you call the startService again. It let you set an action like play,stop,pause music.

public void onStartCommand()
{
     if(intent.getAction.equals("any.play")
     {
        //play song
     }
     else if(intent.getAction.equals("any.stop")
     {}
}
8
  • So when i call a service using startService(intent), will onCreate() wont be called? I have a doubt understanding what is a 'service object' because i have always started a service by startSevice(intent).
    – Diffy
    Oct 25, 2014 at 7:38
  • have you properly instantiate the service object or Probably you don't have the service in your manifest Oct 25, 2014 at 7:44
  • start service like startService(new Intent(this, ServiceName.class)); Oct 25, 2014 at 7:44
  • But is this method called instantiating the service? Where is the service-object then?
    – Diffy
    Oct 25, 2014 at 8:06
  • it is a way of calling Activity and Service using intent is responsible for opening Activity and Service in Android. What you want to do with object of service you can also do it with intent. i mean passing data to Service Oct 25, 2014 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.