Deep analysis of the service and thread differences in Android _android

Source: Internet
Author: User

Service is neither a process nor a thread, and the relationship between them is as follows:

As you can see, their thread IDs are exactly the same, which confirms that the service is actually running in the mainline Chengri, which means that if you write a very time-consuming code in your service, the program will certainly appear ANR .

You may exclaim, this is not a pit dad!? What's the use of my service? In fact, we do not connect the background and child thread together on the line, this is two completely different concepts. The background to 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 still in progress. For example, some applications, always need to maintain a heartbeat connection with the server, you can use the service to achieve. You may ask again, the front is not just verified service is running in the main line Chengri? Do you have a heartbeat connection here that doesn't block the main thread running? Of course, but we can create a child thread in the service and then deal with the time-consuming logic here .

Well, since you want to create a child thread in the service, why not create it directly in the activity? This is because it is difficult for the activity to control the thread, and when the activity is destroyed there is no other way to retrieve the instance of the child thread that was previously created . Also, a child thread created in one activity cannot be manipulated by another. But the service is different, all the activity can be associated with the service, and then it is easy to manipulate the methods, even if the operation is destroyed, and then once again with the service to establish association, It is also possible to obtain an example of binder in the original service. Therefore, the use of service to handle background tasks, the activity can be assured finish, there is no need to worry about the background task cannot control the situation.

The above is a small series for you to bring in-depth analysis of the Android service and thread differences in all the content, I hope to help you, a lot of support cloud Habitat Community ~

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.