开发者

Why use Service for background tasks?

开发者 https://www.devze.com 2023-01-23 07:50 出处:网络
An activity can use AsyncTask or Handler 开发者_如何学运维framework for background work. Both will continue to work even after user has moved away from the activity that started them and the onDestroy

An activity can use AsyncTask or Handler 开发者_如何学运维framework for background work. Both will continue to work even after user has moved away from the activity that started them and the onDestroy for the activity has been called. In other words, an activity is fully capable of doing background work even after its GUI has been shutdown.

In this scenario, use of Service for background work seems like redundancy. What does Service bring to the table that an activity can not do? Thanks.


What is a Service?

Most confusion about the Service class actually revolves around what it is not:

  • A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
  • A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

Thus a Service itself is actually very simple, providing two main features:

  • A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application). This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
  • A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.

Read the rest of the documentation for more info

So one instance of a service would be something you want to happen at set intervals on its own without having to launch an activity or anything else to "launch" it. For example, SMSBackup is just a service that runs in the background, polling every X minutes your SMS messages and copies them into a gmail label, as a "backup" service.


When the UI shown by Activity goes to background(by pressing home button), it is not guaranteed that Activity exists for long time, framework might decide it to stop it.


AsyncTask allows you to easily perform UI updates while also to clearly separate UI thread and working thread logics. However, other applications will not be able to access your AsyncTask. In that case you need the services.


@BraynDenny answer is good enough, I would like to explain in another perspective.

Firstly you misunderstood the meaning of background tasks, definition of service says Service continuously runs in background even if the app is not in foreground, here the term background does not mean separate thread, it means the service runs when you close the application unlike Activity, also service runs in main thread by default, if you want a asynchronous service use intentService

an activity is fully capable of doing background work even after its GUI has been shutdown.

What you said about is absolutely wrong. Activity, Service are components of Android, but AsyncTasks or Threads are not components when you kill an activity the Asynctask in the activity also gets killed since the Asynctask instance is created inside activity class, but when you start downloading a file using foreground Service it is not killed even when you close the application, just like the music applications in Android, even if you close the app the music still plays in the background, showing a foreground notification

There are three different kind of services, to know more about them please check this link https://developer.android.com/guide/components/services.html#CreatingBoundService

0

精彩评论

暂无评论...
验证码 换一张
取 消