Relationship between Service and Thread and how to enable Service, how to disable Service and threadservice

Source: Internet
Author: User

Relationship between Service and Thread and how to enable Service, how to disable Service and threadservice

Relationship between Service and Thread:

Many Android beginners may have such questions: What is the relationship between Service and Thread? When should we use Service and Thread? The answer may surprise you a little because there is no relation between the Service and the Thread! Many people associate them mainly because of the background concept of the Service. Thread we all know that it is used to start a sub-Thread. If you perform some time-consuming operations here, the main Thread will not be blocked. When we first understood the Service, we always thought it was used to process some background tasks, and some time-consuming operations can also be run here, which can be confusing. However, if I tell you that the Service is actually running in the main Thread, do you still think it has something to do with Thread? Let's take a look at this cruel fact. Add a line to the onCreate () method of MainActivity to print the id of the current thread: [java] view plaincopyLog. d ("MyService", "MainActivity thread id is" + Thread. currentThread (). getId (); then add a line to the onCreate () method of MyService to print the id of the current thread: [java] view plaincopyLog. d ("MyService", "MyService thread id is" + Thread. currentThread (). getId (); now run the program again and click Start Service. The following print log is displayed: the thread IDs are identical, this proves that the Service is indeed running in the main thread, that is, if you write a very time-consuming Service Code, the program will certainly appear ANR. You may be surprised. Isn't that a pitfall !? How can I use services? In fact, you can't associate the backend with the sub-thread. This is a completely different concept. The Android background means that its operation is completely independent of the UI. Even if the Activity is destroyed or the program is closed, the Service can continue to run as long as the process is still running. For example, some applications always need to maintain a heartbeat connection with the server, and can be implemented using the Service. You may ask again, didn't you just verify that the Service is running in the main thread? The heartbeat connection is always executed here. Won't the main thread be blocked? Yes, of course, but we can create another sub-thread in the Service and then process the time-consuming logic here. Yes. Since we have to create 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. After the Activity is destroyed, there is no other way to re-obtain the previously created sub-Thread instance. In addition, the sub-thread created in one Activity cannot be operated on by another Activity. But the Service is different. All the activities can be associated with the Service, and then the methods can be easily operated. Even if the Activity is destroyed, you only need to establish a new association with the Service, then you can get the Binder instance in the original Service. Therefore, if you use the Service to process Background tasks, the Activity can be safely completed without worrying about the inability to control background tasks. Start and Stop a service:

Service development is relatively simple, as follows:

Step 1: Inherit the Service class

Public classSMSService extends Service {}

Step 2: configure the service in the <application> node in the AndroidManifest. xml file: <service android: name = ". SMSService"/>

The service cannot run on its own. You must call the Context. startService () or Context. bindService () method to start the service. Both methods can start the Service, but their usage is different. The startService () method is used to enable the service. There is no relation between the caller and the service. Even if the caller exits, the Service continues to run. When the bindService () method is used to enable the Service, the caller and the service are bound together. Once the caller exits, the service is terminated. This feature features that the caller does not want to generate at the same time and must die at the same time.

If you plan to use the Context. startService () method to start the service, when the service is not created, the system will first call the onCreate () method of the service and then call the onStart () method. If the service has been created before the startService () method is called, multiple call of the startService () method will not lead to multiple creation of the service, but will lead to multiple calls of the onStart () method. A service started using the startService () method can only end the service by calling the Context. stopService () method. The onDestroy () method is called when the service ends.

If you plan to use the Context. bindService () method to start the service, when the service is not created, the system will first call the onCreate () method of the service and then call the onBind () method. At this time, the caller and the service are bound together. After the caller exits, the system will first call the onUnbind () method of the service and then call the onDestroy () method. If the service has been bound before the bindService () method is called, multiple calls to the bindService () method will not result in multiple service creation and binding (that is, onCreate () and onBind () method is not called multiple times ). If the caller wants to unbind from the service being bound, he can call the unbindService () method. Calling this method also causes the system to call the onUnbind () --> onDestroy () method of the service.

 

Common lifecycle callback methods for services are as follows:

OnCreate () This method is called when a service is created. This method is called only once. No matter how many times the startService () or bindService () method is called, the service is created only once.

OnDestroy () is called when the service is terminated.

Life cycle method related to starting a service using Context. startService ()

OnStart () calls back this method only when the Context. startService () method is used to start the service. This method is called when the service starts running. Although the startService () method is called multiple times, the onStart () method is called multiple times.

The lifecycle method related to starting a service using the Context. bindService () method

OnBind () calls back this method only when the Context. bindService () method is used to start the service. This method is called when the caller binds to the service. When the caller and the service are already bound, multiple calls to the Context. bindService () method will not cause this method to be called multiple times.

OnUnbind () calls back this method only when the Context. bindService () method is used to start the service. This method is called when the caller and the service are unbound.

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.