A. Basic Knowledge
Services are generally divided into two types:
1: Local Service for internal applications. The service can call Context.startservice () to start and call Context.stopservice () to end. Internally you can call Service.stopself () or Service.stopselfresult () to stop yourself. No matter how many times startservice () is called, only one StopService () is called to stop.
2: Remote service is used between applications within the Android system. Interfaces can be defined and exposed for other applications to operate. The client establishes a connection to the service object and invokes the service through that connection. Call the Context.bindservice () method to establish a connection and start to call Context.unbindservice () to close the connection. Multiple clients can bind to the same service. If the service is not loaded at this time, bindservice () loads it first.
Available for reuse by other applications, such as defining a weather forecast service, and providing calls to other applications.
So let's look at the service life cycle first:
Context.startservice ()->oncreate ()->onstartcommand ()->service running--call Context.stopservice () OnDestroy () context.bindservice ( )
From the appeal can be known respectively corresponding to local, as well as remote, also corresponding to different ways to start the service.
two. precautions
2.1 The same service, multiple launches, the actual process of service execution
The first time you start the service, run OnCreate-->onstartcommand
After the service is started, the service only executes Onstartcommand
During the actual use, the data is passed through intent and executed in Onstartcommand.
Android Service life cycle and usage notes