1.Service
It is one of the four components of Android, and it needs to be registered in XML . It can run in the background for a long time, but there is no user interface.
Even if the user switches between different applications, it can also keep it running. For example, background music playback and so on.
Often used in long-time operations , such as updating contentprovider, sending intent, and initiating system notifications, etc.
Note: The service is not a separate process, or a thread.
2.Service invocation form (can take both forms)
Started: Invokes the activity's StartService method to start. It operates independently and can be called by the StopService method to destroy it.
The bad, cannot activity cannot obtain the service information.
Bound: Call the bindservice method. It binds to the activity that invokes it and communicates with the activity in the form of Client-server .
Mainly through a IBinder object implementation, multiple components can bind the same service at the same time , the service is quite
On the server, other components (even in other applications) can send messages to the service to contact it until all
The component is unbound before it dies.
Note: Regardless of your invocation form, other components (even in other applications) can use the service, but you can privatize the service in XML,
make other components inaccessible. The service is not a thread or process, it is running in the main thread, unless you open another thread to the service and let it focus on its things.
3. Binding
A.activity class
The corresponding service class is specified by intent, and the Bindservice(Intent, SC, bind_auto_create) method is called
Bind the SERVICE,SC to the Serviceconnection class,
B.serviceconnection class
onserviceconnected (componentname name, IBinder Service) method: called with service binding
When binding with the service, accept the binder that the service sent over. onservicedisconnected (componentname name) method: Unbind timed call
When the service is unbound, the corresponding situation is handled
C.service (abstract Class)
OnCreate () Method://Called when the object is created, the object runs until it is destroyed!
OnDestroy () Method://Called when destroying, such as StopService
Onstartcommand method://Invoke when service is started, such as StartService
IBinder Onbind (Intent Intent) method (Overrides must be overridden):
The method is used when the user Bindservice binds to the service when called and returns the IBinder primarily
Communicating with IBinder objects helps the document say it is best to inherit a class from Binder (Class) to return.
4.Demo
Two button, "complete binding": Implements Bindservice operation. "Get Service Information": Get the information returned in IBinder
Activity class
PackageHq.bindservice;ImportHq.bindService.FirstService.CIBinder;Importandroid.app.Activity;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classBindserviceactivityextendsActivity {/**Called when the activity is first created.*/ PrivateButton b=NULL; PrivateButton b2=NULL; PrivateCibinder cb=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); b=(Button) Findviewbyid (R.ID.B); B.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//bind service, any connection is implemented by intent intent, so create intent immediatelyIntent intent=NewIntent (); //immediately shows which service is specified to startIntent.setclass (bindserviceactivity. This, FirstService.class); //The second parameter is serviceconnection, which is used to handle//when binding with the service, accept the binder that the service sent over. //when the service is unbound, the corresponding situation is handledBindservice (Intent, SC, bind_auto_create); } }); B2=(Button) Findviewbyid (R.ID.B2); B2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//get its service through binderFirstService s=Cb.getservice (); //invoking a member method that binds a serviceSystem.out.println ("Cb-get-service-mes" +s.getservicemess ()); } }); } //write an object that implements the Serviceconnection interface. Serviceconnection sc=Newserviceconnection () {//When Bindservice is called, that is, when contacting the service, accept the IBinder that is sent.@Override Public voidonserviceconnected (componentname name, IBinder service) {//TODO auto-generated Method StubSystem.out.println ("onserviceconnected:name=" +name); CB=(cibinder) service; String s=Cb.getextra (); System.out.println ("Onserviceconnected:" +s); } //call to release a binding@Override Public voidonservicedisconnected (componentname name) {//TODO auto-generated Method StubSystem.out.println ("onservicedisconnected:name=" +name); } };}
Subclass that inherits service
1 PackageHq.bindservice;2 ImportJava.io.FileDescriptor;3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.Binder;6 ImportAndroid.os.IBinder;7 ImportAndroid.os.IInterface;8 ImportAndroid.os.Parcel;9 Importandroid.os.RemoteException;Ten One Public classFirstServiceextendsService A { - Private FinalCibinder cb=NewCibinder (); - //The method is used when the user Bindservice binds the service when called and returns the IBinder the //communication is mainly through the IBinder object - @Override - Publicibinder onbind (Intent Intent) { - //TODO auto-generated Method Stub + //Help documentation says it's best to inherit binder (class) - returnCB; + } A //Create a IBinder at classCibinderextendsBinder - { - Publicfirstservice GetService () - { - returnFirstService. This; - } in PublicString Getextra () - { to return"Cbinder:getextra"; + } - the } * PublicString getservicemess () $ {Panax Notoginseng return"Getservicemess"; - } the +}
View Code
android-service-< 11 >