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 can't do that.
Android Learning Development Note the difference between Service and Thread