Basic usage of Android service:
The most basic usage of service is naturally the start and stop operations.
There are two ways to start a service:
1, start by StartService (Intent Intent) mode, automatically execute OnCreate (), Onstartcommand () method.
2. Through Bindservice (Intent intent,serviceconnection connection,int flag)
The first parameter is a intent object, and the second parameter is an instance of the connection service.
The third parameter is a flag bit where the bind_auto_create is passed in,
Indicates that the service is created automatically after the activity and service are established, which causes the OnCreate () method in the MyService to be executed,
However, the Onstartcommand () method does not execute.
There are two ways to destroy a service
1, by StopService (Intent Intent) method to destroy. The OnDestroy () method is called at this time
2, through Unbindservice (Serviceconnection conn);
Note: If a separate operation StartService start and stopservice destroy service or Bindservice start and unbindservice unbind is no problem,
But if StartService and then Bindservice, then StopService or unbindservice alone can not destroy the service.
The service will be destroyed only if all two methods are called. This is because the service is already connected and will be created automatically when the Bindservice is opened.
。 That is, clicking the Stop Service button will only stop the service, and clicking the Unbind Service button will only disassociate the service from the activity.
A service must be destroyed when it is not associated with any activity and the stop state is processed.
In the service, the main thread communicates with the child thread using handler.
Talking about Android Service