The difference between service and Thread in Android [go]

Source: Internet
Author: User

Many times, you may ask, why use the Service, instead of thread, because the thread is very convenient, than the Service is more convenient, I would like to explain in detail below.

1). Thread:thread is the smallest unit of program execution, and it is the basic unit for allocating CPUs. You can use the Thread to perform some asynchronous operations.

2). Service:service is a mechanism for Android, and when it runs, if it is a local service, then the corresponding service is running on the main thread of the master process. such as: Oncreate,onstart These functions are run on the main thread of the master process when they are called by the system. In the case of remote service, the corresponding service is running on the main thread of the standalone process. So please do not interpret the Service as a thread, it has no relationship with the thread of half a dime!

In that case, why should we use Service? Actually this is related to the Android system mechanism, we first take the Thread to say. The thread runs independently of the activity, meaning that if you do not actively stop the thread or the Run method in thread does not complete after an activity is finished, thread will always execute. So there's a problem here: When the Activity is finished, you no longer have a reference to that Thread. On the other hand, you have no way to control the same Thread in different Activity.

For example, if your thread needs to be connected to a server for some sort of synchronization over time, the thread needs to be running when the Activity does not start. This time when you start an activity there is no way to control the Thread that was created in the activity. So you need to create and start a service, create, run, and control the Thread within the service, which solves the problem (because any Activity can control the same service, and the system will only create an instance of the corresponding service).

So you can think of the service as a message, and you can call Context.startservice, Context.stopservice, Context.bindservice,context.unbindservice, to control it, you can also register Broadcastreceiver in the Service, in other places by sending broadcast to control it, of course, these are The Thread cannot do that.

3. Service life cycle

OnCreate OnStart OnDestroy Onbind

1). The life cycle of the service being started: If a service is started by an activity call Context.startservice method, Then the service runs in the background regardless of whether or not activity is unbound to the service using Bindservice bindings or Unbindservice. If a service is started multiple times by the StartService method, the OnCreate method is only called once, OnStart will be called multiple times (corresponding to the number of calls StartService), And only one instance of the service is created (so you should know that only one stopservice call is needed). The service will always run in the background, regardless of whether the program's activity is running until it is called StopService, or its own Stopself method. Of course, if the system resources are insufficient, the Android system may end the service.

2). The lifecycle of a service that is bound: if one service is started by an activity call Context.bindservice method binding, the OnCreate method is only called once, regardless of the number of calls Bindservice calls. The OnStart method is always not called. When the connection is established, the service will run until the call to Context.unbindservice disconnects or the Context of the previous call to Bindservice does not exist (for example, when activity is finished), The system will automatically stop the service and the corresponding OnDestroy will be called.

3). The life cycle of a service that is started and bound: If a service is also started and bound, that service will always run in the background. And no matter how it is called, OnCreate is always called only once, how many times the service's onstart is called, and how many times the StartService is called. Calling Unbindservice will not stop the service, but must call StopService or the stopself of the services to stop it.

4). When the service is stopped, the service is cleared: when one of the services is terminated (1, call stopservice;2, call stopself;3, no longer has a bound connection (not started)), the OnDestroy method will be called, where you should do some cleanup work, such as stopping a thread that is created and running in the service.

Special attention:

1, you should know that when the call Bindservice bound to the service, you should make sure to call Unbindservice in a certain place to unbind (although the Activity by the time the binding will be removed automatically, and the service will automatically stop);

2, you should pay attention to use StartService start the service, must use StopService stop service, regardless of whether you use Bindservice;

3, at the same time use StartService and bindservice to notice, service termination, need to unbindservice and stopservice simultaneously call, can terminate Service, regardless of StartService and The order in which Bindservice is called, if Unbindservice is called first, the service does not terminate automatically, then the service stops after calling StopService, and if StopService is called first, the service will not terminate, and then call Unbindservice or previously called Bindservice the Context does not exist (such as when the activity was finished) after the service will automatically stop;

4, when rotating mobile phone screen, when the phone screen in the "horizontal" "vertical" transformation, at this time if your activity will automatically rotate, the rotation is actually the activity of re-creation, so the rotation before the use of Bindservice established connection will be disconnected (Context does not exist), the life cycle of the corresponding service is the same as above.

5, in the SDK 2.0 and later versions, the corresponding OnStart has been rejected into the Onstartcommand, but the previous OnStart still valid. This means that if you are developing an application with an SDK of 2.0 and later, you should use Onstartcommand instead of OnStart.

6, service is a mechanism of Android, when it is running, if it is the local service, then the corresponding service is running on the main thread of the master process. In the case of remote service, the corresponding service is running on the main thread of the standalone process, and the communication between the service and the caller is synchronous (both remote service and local service), and it has no relationship with the thread!

7, Thread's operation is independent of activity, that is, when an activity is finished, if you do not actively stop the thread or thread in the Run method is not completed, thread will also be executed. So there's a problem here: When the Activity is finished, you no longer have a reference to the thread, which is the next time you start, you can't control the thread you created, and the service can. On the other hand, you have no way to control the same Thread in different Activity.

8, you can also register Broadcastreceiver in the Service, in other places by sending broadcast to control it, these are not the Thread to do.

from:http://blog.csdn.net/jiangwei0910410003/article/details/17008687

The difference between service and Thread in Android [go]

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.