@, Context.startservice (Intent)
Lifecycle Callback:
1, onCreate (...)
2, Onstartcommande (Intent, int, int)
3, OnDestroy ()
Stop service:
1, Service.stopself ()/stopself (int)
2, Context.stopservice (Intent)
@, Context.bindservice
Lifecycle Callback:
1, Onbind (...)
2, Onunbind ()
Stop service:
1, Context.unbindservice () (When all clients perform this operation, the service shuts down automatically, in addition to calling Service.startforeground (). )
@, keep service not to be terminated
Call Startforeground () in the service, which should call Stopforeground () correctly to terminate the service, otherwise the service will run.
@, communicating with activity:
1, use Sendbroadcast (Intent).
2, in the service to add the interface and interface instances and instances of the set method, and the activity to implement this interface. This method can be used in conjunction with the locally Bound service mode, which allows you to invoke the service's method directly in the activity.
@, Intentservice
1, Handlerthread,handler,message mode.
2, inherit intentservice only need to implement Onhandleintent (Intent) method.
3, the above two points determine, use Intentservice no longer create threads, and onhandleintent can only handle one intent at a time, so if there is a long time intent, Other intent of this intentservice may be blocked.
@, custom service implementation parallel service
1. Add the Executorservice instance member to the custom service to perform the task through its excute (Runnable). by Startforeground (), keep the service running in the background, record the current number of tasks by counting member variables, and decide whether to call Stopforeground () to terminate the service based on the current number of tasks.
Note: For an example of a service see Android programming:pushing the Limits--Chapter 6:services and Background Tasks.
Android--Service