Android Development Practice: using Service or thread

Source: Internet
Author: User

Service is one of the four components of Android, is used to perform long-time background tasks, similarly, the thread can also be implemented in the background to perform tasks, what is the difference between them? When do I use a service when I use thread? Today I also say my understanding and summary.


First, you need to understand several features of the service.


(1) By default, the service is actually running in the main thread, and if you need to perform complex time-consuming operations, you must create another thread in the service to perform the task.


(2) The service priority is higher than the background suspended activity, of course, also higher than the thread created by the activity, therefore, the system may be in low memory priority to kill the background activity or thread, and not easily kill the service components , even if forced to kill the service, will restart the killed service when the resource is available

In fact, service and thread are not a level of things at all, service is one of the four components of the system, thread is just a tool class for performing background tasks, it can be created in activity, or it can be created in service. So instead of discussing whether to use service or thread, we should discuss where to create the thread.


In a typical application, it can be created in the following three locations, different locations , whose lifecycles are not the same, so we should decide whether to create a thread in the service or create it in the activity based on the thread's target life cycle.


(1) Created in activity


In this case, it is generally created at OnCreate, destroyed in OnDestroy (), otherwise, the thread will still be running in the background after the activity is destroyed.


In this case, the life cycle of the thread is the life cycle of the entire activity. Therefore, the thread created in activity is only suitable for tasks that depend on the activity itself, such as updating the control state of the activity on a regular basis.


Core features: The thread is for this activity service, complete this specific activity account of the task, and actively notify the activity of some messages and events, the activity destroyed, the thread does not have the meaning of survival.


(2) created in application


In this case, the general custom application class, overloading the OnCreate method, and creating the thread in it, of course, will also destroy the thread in the Onterminate () method, otherwise, if the thread does not exit, Even if the entire application exits, the thread will still run in the background.


In this case, the life cycle of thread is the life cycle of the entire application. So, the thread created in application can perform some of the entire application-level tasks, such as checking the network connection status on a timed basis.


Core Features: The ultimate goal of this thread is for the app's various activity services, including the completion of an activity account of the task, the active notification of some activity some messages and events, and the app exited after the thread does not have the meaning of survival.


In either case, the thread's life cycle should not exceed the lifetime of the entire application-that is, the thread should exit completely after the entire app exits, so that there is no memory leak or zombie thread. Then, if you want the entire app to exit and still be able to run the thread, then you should put the thread in the service to create and start it.


(3) Created in service


This is the only way to guarantee the thread of the longest life cycle, as long as the entire service does not exit, thread can be executed in the background, typically created in the service's OnCreate () and destroyed in OnDestroy ().


Therefore, the thread created in the service is suitable for long-term execution of some app-independent background tasks, and it is common to keep long connections to the server side in the service.


Core features: The thread can provide some "service" or "status query" to the app, but the thread does not need to proactively notify the app of any events, or even need to know who the app is.


In short, we should not consider the use of thread or the service, but should choose the right life cycle for thread, this is my thinking and understanding of service and thread, there is no clear place to welcome messages or letters [email protected] exchange.


This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1547032

Android Development Practices: Using service or 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.