The Android service is something that runs in the background, with the same level of activity. Since the service is running in the background services, then it is not visible, there is no interface of things. You can start a service to play music, or log changes in the location of your geographic information, or start a service to run and listen to some kind of action all the time.
The service, like all other components, runs in the main thread, so it cannot be used to make time-consuming requests or actions. You can open one by one threads in a service and take time-consuming actions in the thread.
So how exactly does the service work?
The usual, first, a little basic knowledge.
A Basic knowledge
Services are generally divided into two types:
1: Local Service for internal applications. The service can call Context.startservice () to start and call Context.stopservice () to end. Internally you can call Service.stopself () or Service.stopselfresult () to stop yourself. No matter how many times startservice () is called, only one StopService () is called to stop.
2: Remote service is used between applications within the Android system. Interfaces can be defined and exposed for other applications to operate. The client establishes a connection to the service object and invokes the service through that connection. Call the Context.bindservice () method to establish a connection and start to call Context.unbindservice () to close the connection. Multiple clients can bind to the same service. If the service is not loaded at this time, bindservice () loads it first.
Available for reuse by other applications, such as defining a weather forecast service, and providing calls to other applications.
So let's look at the service life cycle first:
Context.startservice ()->oncreate ()->onstart ()->service running--call Context.stopservice () OnDestroy ()
Context . Bindservice ()->oncreate ()->onbind ()->service running--call >onunbind (), OnDestroy () From the appeal can be known respectively corresponding to local, as well as remote, also corresponding to different ways to start the service.
Two Actual combat
We can define a local service to inherit services, and then play the media player in this service or record the change in geographic location. Sometimes our service interacts with the activity, so we can define an inner class to return the service, and of course we have to consider that if the services are started in a binding manner, then the inner class can be defined as inheriting the binder and then returning to the local service with the following code.
PackageCom.dongzi;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.media.MediaPlayer;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public classLocalServiceextendsService {Private Static FinalString TAG = "LocalService"; PrivateIBinder binder=NewLocalservice.localbinder (); @Override Publicibinder onbind (Intent Intent) {returnBinder; } MediaPlayer MediaPlayer=NULL; @Override Public voidonCreate () {log.i (TAG,"OnCreate"); //You can start the media player here .//if (mediaplayer==null)//mediaplayer=mediaplayer.create (this, URI); Super. OnCreate (); } @Override Public voidOnStart (Intent Intent,intStartid) {log.i (TAG,"OnStart"); Super. OnStart (Intent, Startid); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {log.i (TAG,"Onstartcommand"); returnStart_sticky; } @Override Public voidOnDestroy () {log.i (TAG,"OnDestroy"); Super. OnDestroy (); } //define content class inheritance Binder Public classLocalbinderextendsbinder{//back to Local serviceLocalService GetService () {returnLocalService. This; } } }
We can tell from above
Define content class Inheritance Binder
Public class extends binder{ // return Local Service LocalService getService () { return LocalService. This ; } }
You can return to this service, and the activity can invoke the service's method through the service.
So how do you start the service? From the above basic knowledge, we know there are 2 ways, as follows:
// Start the service Private void Startcustomservice () { Intent Intent=new Intent (This, LocalService. Class); StartService (intent); }
The 2nd type of binding method:
LocalService localservice=NULL; //start the service with the Bindservice method Private voidBinderservice () {Intent Intent=NewIntent ( This, LocalService.class); Bindservice (Intent,Newserviceconnection () {@Override Public voidonserviceconnected (componentname componentname, IBinder binder) {//call the Bindservice method to start the service, if the service needs to interact with the activity,//The Onbind method returns the IBinder and returns the current local serviceLocalservice=( (Localservice.localbinder) binder). GetService (); //Here you can either prompt the user, or invoke some method of the service} @Override Public voidonservicedisconnected (componentname componentname) {LocalService=NULL; //Here you can prompt the user}}, Context.bind_auto_create); }
When binding the service, need a service connection object, Serviceconnection, once the service is connected, it will call the Onserviceconnected method, we can return our local service object in this method, see the code While the Onservicedisconnected method is called when the service is disconnected, we can clean up some of the service resources.
Next, we will explain some of Aidl's knowledge. These are closely related to the service.
Transferred from: http://www.cnblogs.com/zhangdongzi/archive/2012/01/08/2316711.html
Service of Android Services