Where should I use Service, AsyncTask and Broadcast Receiver?

I am a little confused where in which case I need to use application components such as Service, asyncTask and Broadcast Receiver.

Can someone explain what exactly the difference between them is there and where I need to use these components?

+4
source share
5 answers

AsyncTask is a friendly way to create a new thread that does some work asynchronously.

A broadcast receiver is something like an event handler for system events. It can work in the background and perform an action when something happens, for example, turn off the phone or turn on Wi-Fi.

A service is simply an application that runs in the background (for example, a daemon) and serves information in the application or simply performs tasks.

Sorry for my English, I'm trying to make it clear, but this is not my native language

+7
source

I will get to where I applied these three in my projects:

1.Service: what you want to do in the background without any user interaction. For example, fetching location data continuously or constantly sending some data to your server. You can also use services to complete tasks every few time units. For example, sending 10 minute background updates.

2.AsyncTask: creating a new thread of execution. The best use I've encountered so far is calling a web service. I used the following: AsyncTask for web service calls 1. Display a progress bar in onPreExecute () 2. Turn my web service calls to doInBackground (Params ...) 3.In onPostExecute (Result) updates the user interface or performs some other actions with web service response.

3.BroadCastRecievers are like global receivers of your application. They can listen to both system events, such as a phone reboot, and a custom event in your application. I used them to start the service when restarting the phone, which stopped when we turned off the phone.

+4
source

Let me explain with usecase so that you understand it better -

  • AsyncTask - want to get something from the server or send a message to the server? If we do this in the main thread, the user will not be able to interact with the application. Therefore, Asynctask is used, and it performs network activity in another thread.

  • Service - Want to do something in the background? How to get the location of users every 10 minutes or 1 hour or to alert the user when he crosses a specific area based on location. The service starts the application even when the application does not open (the user can do something else, or the phone is locked, the Service is still running in the background).

  • Broadcast Receiver - Suppose you track a location and save it locally (when the Internet is offline). Not when the Internet gets up, you want to send all of them. Thus, you register in the OS that you want to listen to for this particular event, and get control. Or, when you want the server to know that the device is rebooting, we just need to implement it.

Clear?

+2
source

The service and its local memory variables are loaded into memory and always work

The BroadCast receiver is only guaranteed in memory and works when processing an event.

The Broadcastreceiver transmitter can be removed from memory by the operating system if the memory is low.

+1
source

A "service" is a component that runs in the background without interacting with the user. Each developer can create new Services in their application. Services support true multitasking for Android, as they can work in their own process.

"AsyncTask" encapsulates the creation of threads and handlers. AsyncTask is launched using the execute () method. The execute () method calls the doInBackground () and onPostExecute () methods. The main goal is to download something without user interaction.

A "Broadcast Receiver" is a class that extends BroadcastReceiver and which is registered as a recipient in an Android application through the AndroidManifest.xml file (or through code). You can dynamically register a BroadcastReceiver using the Context.registerReceiver () method. The BroadcastReceiver class defines the onReceive () method. Only during this method will your BroadcastReceiver object be valid, after which the Android system will be able to process the BroadcastReceiver.

+1
source

Source: https://habr.com/ru/post/1413521/


All Articles