Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

starting an asynctask from a broadcast receiver

I want to know if starting up a asynctask from a broadcast receiver considered a bad practice? I basically registered with the C2DM server of google and then when I intercept the onregistered, broadcast receiver, I want to send it to my server.

what is the best way of accomplishing this?

like image 503
Hades Avatar asked Oct 26 '11 08:10

Hades


People also ask

How do I start a broadcast intent?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

How do I send data from BroadcastReceiver to activity?

startActivity(i); Then, in your activity, you will need to getExtra as so: Intent intent = getIntent(); String message = intent. getStringExtra("message");


1 Answers

Yes, this is considered bad practice. That's because if you start AsyncTask from BroadcastReceiver Android may kill your process if onReceive() returned and there is no other active components running.

The correct way would be to start Service from BroadcastReceiver. And this Service should manage AsyncTask. This way Android will be aware about the active component and Android will not kill it prematurely (unless other critical conditions arise, like not enough memory conditions).

like image 155
inazaruk Avatar answered Nov 03 '22 21:11

inazaruk