I am performing a network operation from a BroadcastReceiver. This operation is being performed in the background and the app will not be running when this happens.
Which is better to use? A Service or an AsyncTask?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A
BroadcastReceiverobject is only valid for the duration of the call to onReceive(). Once your code returns from this function, the system considers the object to be finished and no longer active. So it makes sense to just use anAsyncTaskor aThreadhere.Also from the documentation :
What this means is you can
startService()from your broadcast rcvr, pass the data required for your network operation thru an intent. The service will do the network operation. If the service is sticky, you can usepeekService(). But never should youbindService()to a Broadcast rcvr.