The difference between Android entry-service-start,end,bind,unbind

Source: Internet
Author: User

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
  1. Private String isrunning (context context) {
  2. Activitymanager am = (activitymanager) context
  3. . Getsystemservice (Context.activity_service);
  4. list<runningserviceinfo> list = am.getrunningservices (30);
  5. For (Runningserviceinfo info:list) {
  6. if ((Info.service.getClassName ()). Equals ("Com.fred.app.MyService")) {
  7. return "Running ...";
  8. }
  9. }
  10. return "stopped ...";
  11. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.