The relationship between Android service and thread

Source: Internet
Author: User

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's OnCreate () method to print the current thread ID statement:

LOG.D ("myservice" "" "+ Thread.CurrentThread (). GetId ());  

Then add a line to the MyService's OnCreate () method to print the current thread ID statement:

LOG.D ("myservice" "" "+ Thread.CurrentThread (). GetId ());  

Now run the program again and click on the Start Service button to see the following print log:

As you can see, their thread IDs are exactly the same, which confirms that the service is actually running on the mainline thread, which means that if you write very time-consuming code in the service, the program is bound to 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 the activity is destroyed, or the program is closed, the service can continue to run as long as the process is 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.

Well, since you also 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, 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.

A more standard service can be written as:

@Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {      NewThread (NewRunnable () {@Override Public voidrun () {//start performing background tasks}}). Start (); returnSuper.onstartcommand (Intent, flags, Startid); }    classMybinder extends Binder { Public voidstartdownload () {NewThread (NewRunnable () {@Override Public voidrun () {//perform a specific download task}}). Start (); }    }  

The relationship between Android service and thread

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.