Android gives the service a higher priority than inactive activity, and is less likely to be terminated when the system requests resources. If an already-started service is prematurely terminated at run time, the runtime restarts it whenever sufficient resources are available. To address some extreme situations where service termination can significantly affect the user experience, such as a music player, the priority of the service can be upgraded to the same level of activity as the foreground when necessary.
Although the service does not have a dedicated GUI, they are still running in the main thread of the application.
First, create and control service
1. Service Creation
- Create a class that integrates the self service and override the OnCreate and Onbind methods
- Register the service in the manifest file.
2. Execute the service and control its restart
Override the Onstartcommand event handler to perform a task that is encapsulated by the service. In this handler, you can specify the restart behavior of the service. The Onstartcommand method is called when a service is launched through Onstartservice, which executes multiple times during the service's life cycle.
public int Onstartcommand (Intent Intent, int flags, int startid) {
......
return service.start_sticky;
}
The return value controls how the system responds to a restart of the service when the service is terminated when it is run.
- Start_sticky the standard behavior of the restart, the runtime terminates the service after restarting the call to Onstartcommand, but the incoming intent is null, and is used for service handling of its own state and through starts Ervice and StopService are displayed to start and terminate the service.
- A service that is set to this mode will be restarted only if there is an unhandled start call when the Start_not_sticky service is terminated at runtime. Service stops running if no startservice call is made after the service is terminated. More for regular updates or network polling.
- Start_redeliver_intent a combination of the first two modes, when the service is run and terminated, the service is restarted only if there is an unhandled start-up call or if it is terminated before calling Stopself. In the latter case, Onstartcommand is called but does not pass through intent that are not properly processed.
3. Start and stop service
Start Service: StartService (Intent) is shown or hidden via intent
Stop Service:stopservice (Intent)
Call Stopself to terminate the Service:service after it completes the operation or processing. You can pass a parameter without passing a Startid value to ensure that it has been processed for each StartService instance that is currently called.
4. Bind Service to Activity
After the service and activity are bound, activity maintains a reference to the service instance and makes a method call to the service.
The service support bindings need to implement the Onbind method and return the current instance of the bound service.
To bind the service and other components, you need to implement a new serviceconnection. After the connection is established, a reference to the service instance can be obtained by overriding onserviceconnected and onservicedisconnected.
To execute a binding you need to call bindservide in the activity and pass it to him a intet of the service to bind and a serviceconnection instance.
Flag Bits for service and application bindings
- The Bind_adjust_with_activity system adjusts service priorities based on the importance of the ACTIVITY being bound
- Bind_important and bind_above_client The service should also become a foreground process when the client is in the foreground. Bind_above_client means terminating the activity before terminating the service when running at very low memory conditions
- Bind_not_foreground service never has a foreground priority
- Bind_waive_priority binding The specified service should not change the priority of the service
5. Create a foreground service
The service can be set up to run in the foreground through the Startforeground method of the service, but because it interacts with the user, it is necessary to specify a continuous working notification that will be displayed as long as the service is running in the foreground. When the service no longer needs the priority of the foreground run, it can be moved to the background by Stopforeground.
Service of Android four components