1.service services such as Windows below are similar, no user interface, just work in the background, such as music playback, download can use the service
2. How theactivity is bound to the service and how to initiate its own service in the activity:
The service itself cannot start itself, and other methods are called to start the service.
There are two ways to do this:
StartService (): When using this method, the service is not related to the initiator, but simply the start service, if the initiator exits, the service is still running in the background. (such as music playback, even if you quit the program, the music is still playing)
Bindservice (): When using this method, as the name implies, is to bind the initiator and the service together, the initiator exits, the service also stops
3.service Life cycle:
1) life cycle of StartService mode:
When started, call the StartService method –>oncreate () –> OnStart ()
When stopped, call the StopService method –> OnDestroy ()
2) life cycle of Bindservice mode:
Bind timed, call Bindservice method->oncreate () –> onbind ()
Unbind timed, call Unbindservice method –>onunbind () –> ondestory ()
4. If these methods are intertwined, what will happen?
One principle is that the OnCreate method of service will only be called once, that is, how many times you startservice and Bindservice,service are created only once.
1) If BIND is first, then start directly run the service OnStart method, if first start, then bind the time to run the Onbind method directly.
2) If the service runs during the call of Bindservice, then call StopService, the service will not call the OnDestroy method, the service stop is not dropped, can only call Unbindservice, The service will be destroyed.
4) If a service calls StartService multiple times after StartService is start, the service calls the OnStart method multiple times. If you call StopService multiple times, the service will only call the Ondestroyed method once.
5) If a service calls Bindservice multiple times after Bindservice is start, the service will only invoke the Onbind method once. If you call Unbindservice multiple times, an exception will be thrown
5.Activity if you want to invoke the method inside the service, it is generally two ways to start the service together, the code is as follows:
1. Create a class to inherit the service and create an inner class in MyService to inherit the binder:
public class MyService extends Service {@Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method stub return new Mybinde R ();} private void log () {LOG.E ("TAG", "Method from Service");} public class Mybinder extends Binder {public void GetData () {log ();}}}
2. Get the Mybinder object by starting the service and binding service in activity
Intent service = new Intent (this, time.class); StartService (service); Start Services Bindservice (service, Conn, context.bind_auto_create); Super.onstart (); Binding Service
Private Mybinder mybinder;private Serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (ComponentName arg0) {} @Overridepublic void onserviceconnected (componentname name, IBinder Service) {Mybinder = (mybinder) service; When the binding service succeeds, the secondary method is called, and the Mybinder object}};3 is obtained. The method in the service can be called directly through the Mybinder object: Mybinder.getdata ();
Service of Android four components