Detailed explanation of service usage example in Android tutorial
Service life cycle (applicable to 2.1 and above)
1. The startservice runs in the background regardless of whether any activity is bound to the service. Oncreate (if necessary) - > OnStart (int ID, bundle args). If the service is started multiple times, OnStart will call multiple times, but multiple service instances will not be created. Only one stop is required. The service runs in the background until stopservice or its own stopself() or the platform ends due to insufficient resources.
2. Bound by bindservice called by bindservice, the connection establishment service runs all the time. If only bindservice is not startservice, oncreate() is executed and OnStart (int, bundle) is not called; In this case, when the binding is released, the platform can clear the service (after the connection is destroyed, it will be released, and then it will be destroyed).
3. It is started and bound with a life cycle similar to startservice. Oncreate OnStart will be called.
4. Explicit ondestroy() when stopservice is stopped. Or when there is no longer a binding (when it is not started). Stopservice() does not work with bind.
The following is a simple implementation example. Some parts need to be observed with logcat. AcMain.java
main.xml
MyService.java
AnndroidManifest.xml