title: Android IntentService/Service to request/send data to web service and getting the progress to UI
body: <p>On Android basically, I am trying to upload/download data from a web service and have a progress bar that should be rendered showing the progress percentage. </p>
  
  <p>Following is the approach that I implemented along with the reason for considering them:</p>
  
  <ol>
  <li><strong>Chose <code>IntentService</code> to run in background:</strong>
  As per the <a href="http://developer.android.com/guide/components/bound-services.html#Lifecycle" rel="nofollow">link</a> I could not use bound service as it will destroy when the binded Activity/Fragment is destroyed. Hence the selection of <code>IntentService</code> over <code>Service</code>. Move over it would be one time and intent service works on a worker thread so it is much better.</li>
  <li><strong>Broadcast to receive updates:</strong>
  Implementation of <code>LocalBroadcastManager</code> to update the progress bar UI by registering it to listen to the broadcast received from the <code>IntentService</code> described above.</li>
  </ol>
  
  <p>However with this approach, the following issue needs to be addressed:</p>
  
  <p>Sequence of broadcasts is not maintained. The broadcast carries the percentage of progress of the upload/download. But since the sequence is not maintained, there are chances that I receive stale updates (eg: I may receive 30% after 40%). I also tried using <code>sendBroadcastSync</code> method of the <code>LocalBroadcastManager</code> but it doesn't work consistently <em>(I am not very sure on this)</em>. Hence I tried to implement <code>Messenger</code> via <code>Handler</code> however I got to know that the approach will not be able to reconnect to the on going upload/download once the UI is re-created (Activitiy/Fragment is destroyed and created). Basically the reference to the <code>Handler</code> is lost along with the previous activity/fragment and hence the messages are not delivered to the UI for update.</p>
  
  <p>I have been trying to get my way about since a while but I am not able to get it done. Your words of wisdom will be very helpful.</p>