IntentService android download and return file to Activity

Posted by Andrew G on Stack Overflow See other posts from Stack Overflow or by Andrew G
Published on 2012-11-19T22:29:04Z Indexed on 2012/11/19 23:01 UTC
Read the original article Hit count: 142

Filed under:
|
|

I have a fairly tricky situation that I'm trying to determine the best design for. The basics are this:

  • I'm designing a messaging system with a similar interface to email.
  • When a user clicks a message that has an attachment, an activity is spawned that shows the text of that message along with a paper clip signaling that there is an additional attachment.
  • At this point, I begin preloading the attachment so that when the user clicks on it - it loads more quickly.
  • currently, when the user clicks the attachment, it prompts with a loading dialog until the download is complete at which point it loads a separate attachment viewer activity, passing in the bmp byte array.
  • I don't ever want to save attachments to persistent storage.

The difficulty I have is in supporting rotation as well as home button presses etc. The download is currently done with a thread and handler setup.

Instead of this, I'd like the flow to be the following:

  • User loads message as before, preloading begins of attachment as before (invisible to user).
  • When the user clicks on the attachment link, the attachment viewer activity is spawned right away.
  • If the download was done, the image is displayed. If not, a dialog is shown in THIS activity until it is done and can be displayed. Note that ideally the download never restarts or else I've wasted cycles on the preload.

Obviously I need some persistent background process that is able to keep downloading and is able to call back to arbitrarily bonded Activities. It seems like the IntentService almost fits my needs as it does its work in a background thread and has the Service (non UI) lifecycle. However, will it work for my other needs?

I notice that common implementations for what I want to do get a Messenger from the caller Activity so that a Message object can be sent back to a Handler in the caller's thread. This is all well and good but what happens in my case when the caller Activity is Stopped or Destroyed and the currently active Activity (the attachment viewer) is showing? Is there some way to dynamically bind a new Activity to a running IntentService so that I can send a Message back to the new Activity?

The other question is on the Message object. Can I send arbitrarily large data back in this package? For instance, rather than send back that "The file was downloaded", I need to send back the byte array of the downloaded file itself since I never want to write it to disk (and yes this needs to be the case).

Any advice on achieving the behavior I want is greatly appreciated. I've not been working with Android for that long and I often get confused with how to best handle asynchronous processes over the course of the Activity lifecycle especially when it comes to orientation changes and home button presses...

© Stack Overflow or respective owner

Related posts about android

Related posts about service