Home » Android » android – starting an asynctask from a broadcast receiver

android – starting an asynctask from a broadcast receiver

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&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).

Answer:

Starting with Honeycomb, you can call goAsync(), and then you have 10 seconds or so to do your work asynchronously .

Example of usage can be shown here.