First of all, we need to make sure that the Service is running on the main thread and cannot have time-consuming operations. In this way, when processing time-consuming operations in the Service, we still need to use threads to handle it.
Since you also need to create a child thread in the Service, why not create it directly in the Activity?
This is because it is difficult for Activity to control Thread. When Activity is destroyed, there is no other way to re-get the instance of the previously created child thread. And a child thread created in one activity cannot operate on it. But Service is different. All activities can be associated with Service, and then the methods can be easily operated. Even if the Activity is destroyed, as long as you re-associate with Service, you can obtain the Binder instance in the original Service. Therefore, using Service to handle background tasks, Activity can be completed with confidence without worrying about not being able to control background tasks.
Standard Service writing:
@Override public int onStartCommand(Intent intent, int flags, int startId) { new Thread(new Runnable() { @Override public void run() { // Start execution of background tasks} }).start(); return (intent, flags, startId); } class MyBinder extends Binder { public void startDownload() { new Thread(new Runnable() { @Override public void run() { // Perform specific download tasks} }).start(); } }
The above is the introduction to the difference between Android Service and Thread introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!