Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where I should use Service , AsyncTask and Broadcast Receiver?

I'm in little bit confusion where in what case I need to use application components like Service, asyncTask and Broadcast Receiver.

Can any one explain what the exact difference between these there and where I need to use these components?

like image 770
Bamadeva Avatar asked Dec 07 '22 14:12

Bamadeva


2 Answers

AsyncTask is a friendly way to create a new thread that performs some work asynchronusly.

A Broadcast Receiver is something like an Event Handler for system events. It can run in background and perform an action when something happens, like turning the phone off or turning wifi on..

A Service is just an app that works in background (like a daemon) and serves information to an app or just performs tasks.

Sorry for my English, I try to let me understand but it is not my mother tongue

like image 158
Sebastian Breit Avatar answered Dec 12 '22 05:12

Sebastian Breit


I will get straight to where I have applied these three in my projects so far:

1.Service:Something you want to perform in the background without any user interaction.For instance fetching location data continuously or sending some data continuously to your server.You can also use services to perform tasks every few time units.For example sending ten minute background updates.

2.AsyncTask:Making a new thread of execution.Best use I have encountered so far is calling a web service..I did the following using an AsyncTask for web service calls 1.Display Progress bar in onPreExecute() 2.Perform my web service calls in doInBackground(Params...) 3.In onPostExecute(Result) update the UI or do some other stuff with the response from the web service.

3.BroadCastRecievers are like global recievers for your app.They can listen for both System events like a phone restart or a custom event within your app.I used them for starting a service when the phone was restarted,which stopped when we switched off the phone.

like image 45
Mukund Samant Avatar answered Dec 12 '22 06:12

Mukund Samant