The Android system also provides a component called a "Service" that typically runs in the background. Activity can be used to start a service,service that can remain in the background after startup, even if the activity that initiates it exits or switches to another app service to remain operational.
A 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 initiated it, and can be run in the background for a long time. Typically, this service performs a time-consuming operation in the background (the following download) does not return results to the activity that initiated 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 service to be invoked to interact with the service: Send requests, obtain results, and support interprocess communication. The life cycle of a general bound service is the same as the activity that initiates it, and multiple activity can bind a service at the same time. When all activity is disconnected from the service binding. Service ends automatically.
Although the service can have the above two representations, this is only to illustrate the convenience, in fact, the same service can exist in both ways, as long as the implementation of the two methods defined by the interface function can be.
To create a service, you first need to define a subclass of the service and then overload the service definition with some of the core methods as needed:
- Onstartcommand () When an activity calls StartService, the Android system calls the service's Onstartcommand () method, As mentioned earlier, a service that starts with StartService runs long in the background and is not controlled by the activity that initiates it, so the application has a responsibility to stop service,service and can call stopself to stop itself.
- Onbind () When an activity uses bindservice () to bind a service, the Android system invokes the service's Onbind method, and Onbind needs to return a Ibind object to the caller (Client). The client can then use the method provided by Ibind to use the service.
- The OnCreate () service was called the first time it was created, similar to the activity's onCreate.
- Called when the OnDestroy () service exits.
Once you have defined the service class and implemented the appropriate method, as with activity, you need to define the service in Androidmanifest.xml:
<manifest ... > ... <application ><service android:name= ". Exampleservice "/> ... </application></manifest>
As with activity, you can define intent Filter for the service, and if you do not want to share the service, you can define the android:exported property as false.
Typically the service runs in the background and when Android also supports service running in the foreground , the service running in the foreground must be at the top of the screen status Bar provides a notification to prompt the user to have a service running. 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 running in the foreground, and the service running in the foreground can make the service run in the foreground by calling Startforeground (). Stopforeground stops the foreground operation, but the service itself does not stop. Startforeground,stopforeground is supported from 2.0, with the previous version using Setforeground.
In order to support versions prior to 2.0 and 2.0, the reflection method is used to find out whether the current version contains Startforeground and Stopforeground, and if so, the setforeground is not used.
If found, the following variables are used to store the Startforeground and Stopforeground methods. And this service is not related, it is not detailed. Just know Startforegroundcompat and Stopforegroundcompat function is Startforeground and stopforeground on the line.
Private Static Finalclass[] Mstartforegroundsignature =Newclass[] {int.class, Notification.class};Private Static Finalclass[] Mstopforegroundsignature =Newclass[] {Boolean.class}; PrivateMethod Mstartforeground;PrivateMethod Mstopforeground;Privateobject[] Mstartforegroundargs =NewObject[2];Privateobject[] Mstopforegroundargs =NewOBJECT[1];
Here's a look at the code for Foregroundservice:
First, you must be a subclass of the service:
Public class extends Service
Because it is designed as a "Started" service, you need to define Onstartcommand, and Onstartcommand is also added after Android 2.0, before 2.0 is onstart. In order to support all versions, the two methods are implemented, corresponding to the version after 2.0, only the onstart will be called before the onstartcommand,2.0 is called:
//This was the old OnStart method .//'ll is called on the pre-2.0 platform.//On 2.0 or later we override Onstartcommand () so this//method won't be called.@Override Public voidOnStart (Intent Intent,intStartid) {Handlecommand (intent);} @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {Handlecommand (intent);//We want this service to//continue running until it is explicitly//stopped, so return sticky.returnStart_sticky;}
The Onstartcommand can have a return result , which tells the Android system to follow up when the service is killed (such as when the system is running out of memory). Start_sticky indicates that after the system kill this service, if the service is re-created when Onstartcommand is called, the last intent is not passed in as a parameter, that is, intent=null. Start_redeliver_intent will pass in the last intent that was not processed before being killed.
This service is not used as a bind service, so it is implemented by an empty implementation:
@Override Public ibinder onbind (Intent Intent) { returnnull;}
Finally, to see how to start/stop this service, controller is implemented as the control class for this service, provides foreground boot, background start, and stop service operation:
PrivateOnclicklistener Mforegroundlistener=NewOnclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (Foregroundservice.action_foreground); Intent.setclass (Controller. This, Foregroundservice.class); StartService (Intent); }}; PrivateOnclicklistener Mbackgroundlistener=NewOnclicklistener () { Public voidOnClick (View v) {Intent Intent=NewIntent (Foregroundservice.action_background); Intent.setclass (Controller. This, Foregroundservice.class); StartService (Intent); }}; PrivateOnclicklistener Mstoplistener =NewOnclicklistener () { Public voidOnClick (View v) {StopService (NewIntent (Controller. This, Foregroundservice.class)); }};
"Sail Plan 033" 2015 sail plan Android Apidemo The devil pace of App->service->foreground service Controller service use, shared service, front desk service, Onstartcommand