Summary of the relevant Android Service

A netizen comments from the

1, use

startService start of service is mainly used to start a service to perform background tasks, do not communicate. Stop service using stopService
bindService start of the service which started service to communicate. Stop service using unbindService
startService also bindService start of service out of service should use stepService and unbindService
in three ways above to start its life cycle services are also differences, which will be given subsequently.

2, the difference between the Service and Thread

In many cases, you might ask, Why Service, instead Thread it, because it is convenient to use Thread, compared to Service
also much more convenient, detailed below me explain.

1) Thread:. Thread execution is the smallest unit, which is the basic unit of allocation of CPU. Thread can be used to perform some asynchronous operation.

2) Service:. Service is a mechanism android, and when it runs if it is Local Service, then the corresponding Service is running on the main thread of the main process. Such as: onCreate, onStart these functions are running on the main thread in the main process is invoked system. If RemoteService, then the corresponding Service is running on the main thread is independent of the process. So do not understand the Service as a thread, the thread it with half dime had nothing to do! If so, then why do we use the Service it? In fact, this mechanism related with the android system, we acquire Thread it. Thread is run independent of Activity, which means that when an Activity after being finish, if you do not take the initiative to stop Thread or Thread in the run method has not been completed, then, Thread also been implemented. So there will be a problem: when the Activity is finish, you no longer hold the Thread of reference. On the other hand, you have no way to control the same in a different Thread Activity in. For example: If you need to keep the Thread will connect to the server from time to time to do some kind of synchronization, then the Thread Activity no need to start time is also running. This time when you start an Activity is no way before the Activity which controls the creation of Thread. So you will need to create and start a Service, which is created in the Service, and to control the operation of Thread, this will solve the problem (because any Activity can be controlled by the same Service, and the system will only create an instance of the corresponding Service). So you can imagine the Service as a messaging service, and you can call Context.startService, Context.stopService, Context wherever Context of.

3, Service life cycle

onStart onDestroy onBind the onCreate
1) is started life cycle of services: If a Service is called an Activity Context.startService way to start, then regardless of whether the use of bindService Activity unbindService binding or unbinding to the Service, the Service will running in the background. If a Service is started multiple times startService method, the onCreate method is only called once, onStart will be called multiple times (corresponding to the number of calls startService), and the system will only create an instance of Service (so you should only need to know one stopService call). The Service will always run in the background, regardless of the corresponding program Activity is running, until called stopService, or their stopSelf method. Of course, if enough system resources, android system is also likely to end service.
2) bound the life cycle of services: If a Service Context.bindService method is called an Activity binding start, regardless of the call bindService call several times, onCreate method will only be called once, but will not always be the method onStart transfer. When the connection is established, Service will always run, unless you call Context.unbindService disconnected or before the call bindService of Context does not exist (such as when the Activity of the finish), the system will automatically stop Service, onDestroy correspondence will be called.
. 3) is activated and bound the life cycle of services: If a Service has been started and is bound, the Service will be running in the background. And regardless of the call, onCreate always only called once, the corresponding startService call many times, Service of onStart will call many times. UnbindService calls will not stop Service, and must call stopSelf stopService or Service to stop the service.
4) Clear service when the service is stopped: When a Service is terminated (1, call stopService; 2, call stopSelf; 3, there is no longer bound connection (not activated) when), onDestroy method will be called where you should do some cleanup work, such as stopping create and run the thread in the Service.

pay attention:

1, you should know that when you call bindService bound to the Service, you should ensure that calls unbindService somewhere unbind (although

Activity is finish automatically when the binding is released, and the Service will automatically stop);

2, you should pay attention to the use of startService start the service after, be sure to use

stopService out of service, regardless of whether you use bindService;

After 3 while using startService and bindService to note, Service termination, with needs unbindService stopService calls at the same time, in order to terminate the Service, regardless of the call and order startService bindService if unbindService first call at this time does not automatically terminate the service, then call stopService services will stop, if the first call stopService service at this time will not be terminated, and then call unbindService or bindService before calling the Context does not exist after (such as Activity is finish time) service will automatically stop;
4, when when rotating the phone screen when the phone screen in the "horizontal", "vertical" transformation, this time if your Activity If you will automatically rotate, rotate the fact is to recreate the Activity, so using previous rotation bindService established will disconnect connection (Context does not exist), corresponding to the service life cycle described above.
5, in sdk version 2.0 and later, corresponding to the changed onStart onStartCommand been rejected, but before any natural onStart effective. This means that if you develop applications using sdk 2.0 and later versions, you should use onStartCommand instead onStart.

Guess you like

Origin www.cnblogs.com/wotoufahaiduo/p/11823876.html