What is service:
Service. The name is similar to the "service" that we normally understand. It runs in the background and can interact with each other. It is similar to the activity level,
You cannot run it on your own. You need to call it through an activity or other context objects, including context. startservice () and context. bindservice ().
The two methods for starting a service are different:
If you do some time-consuming things in oncreate or onstart of the Service, it is best to start a thread in the service to complete it, because the service is running in the main
In the thread, it will affect UI operations or block other things in the main thread.
When do I need a service:
For example, when the user starts other activities during multimedia playback, the program will continue playing in the background, for example, detecting file changes on the SD card, or recording in the background
Change the location of your geographic information, etc. In short, services are always hidden in the back.
How to Use Service:
Service call
Context. startservice (): the service will go through oncreate-> onstart (if the service is not running, Android first calls oncreate () and then calls onstart (); if the service is already running, only onstart () is called, so the onstart method of a service may be called multiple times). When stopservice is called, ondestroy is called directly. If the caller directly exits without calling stopservice, the service is always running in the background. After the service caller starts up again, the service can be closed through stopservice.
Note that context is called multiple times. startservice () is not nested (even if the corresponding onstart () method is called), no matter how many times the same service is started, Once context is called. stopservice () or stopself. Note: intent objects passed to startservice () are passed to the onstart () method. The call sequence is oncreate --> onstart (which can be called multiple times) --> ondestroy.
Context. bindservice (): the service will go through oncreate ()-> onbind (), onbind will return to the client an ibind interface instance, ibind allows the client to call back the service method, for example, you can obtain the service running status or other operations. At this time, the caller (such as activity) will be bound with the service, and the context will exit, and srevice will call onunbind-> ondestroyed to exit accordingly, the so-called binding together will survive.
Note: intent objects passed to bindservice () will be passed to onbind (), intent objects passed to unbindservice () will be passed to onunbind () method. The call sequence is oncreate --> onbind (only once, cannot be bound multiple times) --> onunbind --> ondestory.
Note: during each service enabling and disabling process, only onstart can be called multiple times (through multiple startservice calls), other oncreate, onbind, onunbind, ondestory can only be called once in a lifecycle. Another point is that I have never encountered a situation where the interaction between startservice and bindservice is required (I don't think there will be such a requirement), so I don't have to consider the issue of interaction, so it is not too late to consider the treatment.
Adcastreceiver can only start the service through startservice, because the broadcast itself has a short life cycle, BIND is meaningless
Service lifecycle:
Service has fewer lifecycle methods than activity. Only oncreate, onstart, and ondestroy are supported to start a service.
The impact of lifecycle is different.
1. Use startservice
The Service will go through oncreate-> onstart stopservice directly ondestroy
If the caller (testserviceholder) directly exits without calling stopservice, the Service will continue to run in the background.
The next time testserviceholder gets up again, it can be stopservice.
2. bindservice
The service only runs oncreate. At this time, testserviceholder and testservice are bound together.
When testserviceholder exits, srevice will call onunbind-> ondestroyed to bond together to survive.