Service: Services
Service is one of the core components of the activity system. Hence the need to inherit and register
Service is an internal interface, suitable for long-term execution of tasks in the background. (such as singing, detection version with new, download, upload, etc.)
Although the service is suitable for long-running tasks, the service runs on the main thread. So time-consuming operations in the service need to turn on sub-threads to implement
Service life cycle:
1. Turn on the service
OnCreate () The first activation is called
Onstartcommand () will be called every time the activation is activated.
2. Turn off the service
OnDestroy ()
To turn service on and off:
Open:
Intent Intent = new Intent (context, xxxservice.class);
StartService (Intent);
Shut down:
Intent Intent = new Intent (context, xxxservice.class);
StopService (Intent);
Stickiness of Service
When the service component is terminated unexpectedly, it is restarted automatically over a period of time. This feature is called the stickiness of the service.
The stickiness of the Service is determined by the return value of the Onstartcommand () method. The return value can be expressed using a constant,
The values are:
Start_sticky: Sticky
Start_sticky_compatibility: Sticky in compatibility mode
Start_not_sticky: Non-sticky
Start_redeliver_intent: Sticky, and resend the INTENT object used to activate the service
Android Core Component Service