Write a section of others on the service of start and bind,end and unbind analysis:
Service creation is available in two ways: StartService or Bindservice
The service cannot run itself and needs to start the service by calling the Context.startservice () or Context.bindservice () method. Both of these methods can start the service, but they are used in different situations.
The service is enabled with the StartService () method, the caller is not connected to the service, and the service is still running even if the caller exits.
Using the Bindservice () method to enable the service, the caller and the service are bound together, and once the caller exits, the service terminates, and there is a "no need to live at the same time Must Die" feature.
If you plan to start the service with the Context.startservice () method, the system calls the service's OnCreate () method first, and then calls the OnStart () method when the service is not created. If the StartService () method is called before the service has been created, calling the StartService () method multiple times does not cause the service to be created more than once, but results in multiple calls to the OnStart () method. Services started with the StartService () method can only call the Context.stopservice () method to end the service, and the OnDestroy () method is called at the end of the service.
If you plan to start the service with the Context.bindservice () method, the system calls the service's OnCreate () method first, and then calls the Onbind () method when the service is not created. This time the caller and the service are bound together and the caller exits, the system calls the service's Onunbind () method first, and then calls the OnDestroy () method. If the service is already bound before the Bindservice () method is called, multiple calls to the Bindservice () method do not cause the service and bindings to be created multiple times (that is, the OnCreate () and Onbind () methods are not called multiple times). If the caller wants to unbind the service being bound, it can call the Unbindservice () method, and calling the method will also cause the system to invoke the service's Onunbind ()-->ondestroy () method.
Look like a very complex look, in fact, there is a relatively simple understanding, that is:
(1) plain difference between start and bind;
(2) Start service can be End,bind service end cannot let him destory;
(3) Bind service must be unbind to destroy;
(4) The service can bind multiple times (as if it doesn't make sense), but only unbind once.
(5) Cannot unbind service that is not bind.
By the way, how to see if a service is running:
[Java]View Plaincopy
- Private String isrunning (context context) {
- Activitymanager am = (activitymanager) context
- . Getsystemservice (Context.activity_service);
- list<runningserviceinfo> list = am.getrunningservices (30);
- For (Runningserviceinfo info:list) {
- if ((Info.service.getClassName ()). Equals ("Com.fred.app.MyService")) {
- return "Running ...";
- }
- }
- return "stopped ...";
- }
I have an example here, a small partner can download the run to see, you can probably know the difference.
http://download.csdn.net/detail/mrdemigod/6453585
The difference between Android entry-service-start,end,bind,unbind