App->service->foreground Service Controller
Like the service in Windows, the Android system also provides a component called "service" that normally runs in the background. An activity can be used to start a service,service startup to keep running in the background, even if the activity that initiates it exits or switches to another application service.
Service can exist in two ways:
Started when an activity uses StartService () to start a service, once the service is started, it is not controlled by the activity that initiates it, and can run for a long time in the background, Typically, the service performs a time-consuming operation in the background (the following loading) does not return results to the activity that initiates it.
Bound uses Bindservice () for activity or other program parts to start the service. The Bound service provides a Client/service method that allows the activity of the calling service to interact with the service: sending requests, obtaining results, and supporting interprocess communication. The life cycle of a general bound service is the same as the activity that initiates it, and multiple activity can bind to a service at the same time. When all activity is disconnected from the service. The service ends automatically.
Although service can have both of these manifestations, this is only to illustrate the convenience of, in fact, the same service can be in two ways, as long as the implementation of the two methods defined interface functions can be.
To create a service, you first need to define a subclass of the service, and then overload the service definition with some core methods as needed:
Onstartcommand () When an activity calls StartService, the Android system invokes the Onstartcommand () method of the service, as stated earlier A service that starts with StartService will run long in the background and not be controlled by the activity that initiates it, so the application has the responsibility to stop the service, and the service can call stopself to stop itself.
Onbind () When an activity uses bindservice () to bind a service, the Android system invokes the Onbind method of the service, Onbind needs to return a Ibind object to the caller (Client). The client can then use the service by using the method provided by Ibind.
The OnCreate () service was invoked the first time it was created, similar to the onCreate of the activity.
Called when the OnDestroy () service exits.
After defining the service class and implementing the appropriate method, as with the activity, you also need to define the service in Androidmanifest.xml:
<manifest ... >
...
<application ... >
<service android:name= ". Exampleservice "/>
...
</application>
</manifest>
As with activity, you can also define intent Filter for service, and if you do not want to share the service, you can define the Android:exported property as false.
Normally the service runs in the background, and when Android also supports service running at the front desk, the service running at the front must provide a notification at the top of the screen at status bar to prompt the user for a service to run. For example, provide a service that media Player uses to run in the foreground and display the current track in the title bar.
This example foreground service Controller shows a service that runs in the foreground, and the service at the front can run the service in the foreground by calling Startforeground (). Stopforeground stops the foreground running, but the service itself does not stop. The startforeground,stopforeground is supported from 2.0, and the previous version uses Setforeground.
In order to support versions prior to 2.0 and 2.0, this example uses a reflection method to find out whether the current version contains Startforeground and Stopforeground, if so, without or using Setforeground.
If found, the following variables are used to store the Startforeground and Stopforeground methods. And this case service is not related, it is not detailed. As long as you know Startforegroundcompat and Stopforegroundcompat function is Startforeground and stopforeground on the line.
private static final class[] Mstartforegroundsignature = new class[] {
Int.class, Notification.class};
private static final class[] Mstopforegroundsignature = new class[] {
Boolean.class};
Private method Mstartforeground;
Private method Mstopforeground;
Private object[] Mstartforegroundargs = new object[2];