Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:44:14+00:00 2026-06-15T10:44:14+00:00

Ok, I’ve been researching the best way to solve this problem for some time

  • 0

Ok, I’ve been researching the best way to solve this problem for some time so let me break down my needs and findings for you.

Needs:

  • An Activity [SendMsg] composes a message body which may include attachments of images up to 5MB.
  • When the user sends this message, the message must be uploaded to my server
  • This upload must happen synchronously in the sense that I block the user from using the app until it has completed or failed
  • When complete, the SendMsg Activity is destroyed if the send was successful – otherwise an error dialog is displayed
  • The upload and user experience must persist given any phone rotation or home button presses / phone calls etc

The last bullet here turns out to be the hardest.

findings:

I thought about just doing the work in a static AsyncTask but I don’t think that works for me – state gets really messy and it seems as though I have no guarantee the OS doesn’t kill the thread when the app is in the background.

I decided to go with a Service in which it is created and bound/unbound from the Activity. The service spawns an AsyncTask, does the work and then signals the Activity according to its result (SUCCEED/FAIL). This paradigm seems to work fine except for my large messages. The Service/Activity communication Message communication is not made for marshalling such large payloads. *NOTE: if it is not clear, when the user hits send, this form data and file must be made available to the Service. In some cases this is an image taken from the camera which DOES not and CAN not exist on disk for purposes of the app.

Clearly what I want here is some sort of shared memory store where I can save the data from the Activity. Then the Activity can signal the Service that it can go ahead and upload that data.

Can such a shared area exists in memory? Can I get the Application object from a Service and store the data there?

The other problem comes about when the upload finishes but no Activity is bound to the Service but I think I may have figured this one out. Please let me know if the following makes sense:

  1. Service finishes upload
  2. Service checks to see if an Activity is currently bound
  3. If yes, return error Message which Activity handles via its Message queue
  4. If no, store the return code in that shared memory area and flag a boolean there as well
  5. When a client Activity is created / resumed, it should first always check this boolean to see if the process is already complete. If so, dispatch a Message it its own handler and handle the code as before
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:44:15+00:00Added an answer on June 15, 2026 at 10:44 am

    To deal with your memory requirement, I would probably use a Map of UUID to a holder class that has all of your message details. Store this Map as a member variable in your Application subclass, or as a static variable. Make sure to clear this out when your task completes or fails. If you are sure you won’t have multiple requests of this type going at the same time, maybe you can just use a single class instead of a Map or list.

    Then, you can accomplish your requirements using an IntentService, with help from a WakeLock. I tend to avoid bound Services in Android, because they are difficult to work with due to screen rotation. Just pass in the identifier from the Map in the Intent that starts the IntentService.

    In my opinion, the best way to communicate from your Service back to your Activity is to use a PendingIntent. Basically, you start the Service from your Activity, and pass in a PendingIntent in the Intent that starts the Service. When your Service has finished its work, or failed, you call send() on the PendingIntent. Then, your Activity will get a callback in its onActivityResult() method.

    Doing this has several advantages:

    • It’s trivially thread safe. You get a long running task done in a separate thread in the IntentService, and you get notified on the UI thread when it is finished.
    • This supports screen rotation! You can rotate your Activity all you want, and you’ll still get the callback in onActivityResult(). Conversely, even if you finish() your Activity and start a new one before the PendingIntent is sent, it will not be delivered, which is almost always what you want.

    I have a demo application that shows this approach. Basically, you have an Activity which stars some work, and blocks the UI with a progress dialog. This dialog will be shown again if the screen gets rotated. The work is done in the Service, which sends the PendingIntent back to the Activity.

    The last part of your job is to ensure that the device does not go to sleep while your upload is running. Mark Murphy has written a nice example and Service implementation that uses a WakeLock to ensure work gets done uninterrupted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have been unable to fix a problem with Java Unicode and encoding. The
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.