On Mon, Jul 12, 2010 at 12:29 PM, Matt <[email protected]> wrote:
> As you should know, you should not be running code that takes a long
> time to complete, such as a network download, in a BroadcastReceiver.
> The reason is that it is run on the "main thread" and will cause an
> ANR (Application Not Responding) if the code does not complete within
> a short amount of time.  The proper solution is to handle this long
> running code on a background thread.
>
> I've seen conflicting comments about whether or not it is safe to use
> an AsyncTask from a BroadcastReceiver (btw, an AppWidget is a child of
> BroadcastReceiver, so we can use the two terms interchangeably).  On
> the one hand, I've heard that you should only use a Service, because
> the thread forked by AsyncTask can be killed, along with the
> BroadcastReceiver thread, once the BroadcastReceiver's onReceive() is
> finished due to Android killing the process.  On the other hand, I've
> actually seen code that simply uses an AsynchTask from an AppWidget to
> download background data.
>
> If the first point is correct, then it would imply that the second
> point is incorrect... unless there is already an existing background
> service which keeps the process alive, which I don't know about.

Personally, I'd rather you use an IntentService than an AsyncTask
launched from a BroadcastReceiver. It is not that much harder (same
approximate LOC, just with one more manifest entry). Like AsyncTask,
IntentService handles the background thread, and it automatically goes
away once there is no more work to be done.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android App Developer Books: http://commonsware.com/books

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to