Service and thread relationships and how to enable service, how to deactivate service

Source: Internet
Author: User

Service and Thread relationships:

Many Android beginners may have such doubts, service and thread in the end what is the relationship? When should I use the service and when should I use thread? The answer may be a bit of a surprise to you, because there is no relationship between service and thread!   There are a lot of people who will connect them, mainly because of the service background concept. Thread as we all know, is used to open a sub-thread, here to perform some time-consuming operation will not block the main thread running. When we first understand the service, we always feel that it is used to deal with some background tasks, and some more time-consuming operations can be put here to run, which makes people confused. But if I tell you that the service is actually running on the mainline thread, do you still think it has anything to do with thread? Let's take a look at this cruel fact.   Add a line to the Mainactivity OnCreate () method to print the current thread ID statement:  [java] View plaincopylog.d ("MyService", "mainactivity Thread ID is "+ thread.currentthread (). GetId ());   then adds a line to the MyService's OnCreate () method to print the current thread ID statement: [Java] View plaincopylog.d ("MyService", "MyService thread ID is" + Thread.CurrentThread (). GetId ());   Now rerun the program and click on the Start Service button to see the following print log:              /                    You can see that their thread IDs are exactly the same, which confirms that the service is actually running on the mainline thread, In other words, if you write very time-consuming code in the service, the program will certainly appear ANR.   You may exclaim, this is not the pit father!? What's the use of the service? In fact, we do not have to link the background and sub-threads together on the line, this is two completely different concepts. The background of Android is that it runs completely independent of the UI. Even if ActiviTy is destroyed, or the program is shut down, and the service can continue to run as long as the process is still in progress. For example, some applications, which always require a heartbeat connection to the server, can be implemented using the service. You may also ask, is it not just verified that the service is running on the mainline thread? Is there a heartbeat connection going on here that doesn't block the main thread running? Of course, but we can create a sub-thread in the service and then deal with the time-consuming logic here.  , now that you're creating a sub-thread in the service, why not create it directly in the activity? This is because the activity is difficult to control the thread, and when the activity is destroyed, there is no other way to regain the instance of the previously created child thread. and a child thread created in one activity, another activity cannot manipulate it. But the service is different, all the activity can be associated with the service, and then it is easy to manipulate the method, even if the activity is destroyed, and then just re-associated with the service, It is also possible to obtain an instance of binder in the original service. Therefore, using the service to handle background tasks, activity can safely finish without worrying about situations where background tasks cannot be controlled.   Start and stop service:

The development of the service is relatively simple, as follows:

First step: Inheriting the service class

Public Classsmsservice extends Service {}

Step Two: Configure the service in the <application> node in the Androidmanifest.xml file: <service android:name= ". SMSService "/>

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.

The service common life cycle callback method is as follows:

OnCreate () This method is called when the service is created, and the method is only called once, and the service is created only once, regardless of how many times the StartService () or Bindservice () method is called.

OnDestroy () This method is called when the service is terminated.

Life cycle methods related to starting services with the Context.startservice () method

OnStart () The method is only invoked when the service is started with the Context.startservice () method. This method is called when the service starts running. The StartService () method is called multiple times although the service is not created more than once, but the OnStart () method is called multiple times.

Life cycle methods related to starting services with the Context.bindservice () method

Onbind () The method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are bound, and the Context.bindservice () method is called multiple times when the caller is tied to the service and does not cause the method to be called more than once.

Onunbind () The method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are unbound

Service and thread relationships and how to enable service, how to deactivate service

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.