title: How to Update progress bar in status bar while file upload occurs using a Service?
body: <p>I have researched this and not found a clear answer that has a combination of progress bar + status bar + Service? My issue is that the way file upload is implemented, (that being the only way it can because the server supports it like this), I cannot keep track of number of bytes being written. Hence, I cannot increment the progress bar correctly to reflect the accurate file upload status.
  The current implementation increments the progress bar, does the upload and removes the progress bar once it reaches 100. BUT that progress is not accurate reflection of the upload. The upload happens much earlier yet the bar keeps ON. I would rather have the progress showing while this request/response is occurring.
  Following is my code snippet:</p>
  
  <pre><code>  public class UploadFileService extends IntentService {
      public UploadFileService() {
  super("UploadFileService");
  }
  
  @Override
  protected void onHandleIntent(Intent intent) {
      fName = intent.getExtras().getString(Constants.EXTRA_K_FILENAME);
      path = intent.getExtras().getString(Constants.EXTRA_K_FILEPATH);
              attribute = intent.getExtras().getString(Constants.EXTRA_K_ATTRIBUTE);
  
  //Start thread to doFilepUpload
  uploadthread = new UploadThread(fName, path, attribute);
  uploadthread.start();
  </code></pre>
  
  <p>}</p>
  
  <p>// Define the Handler that receives messages from the thread and update the progress</p>
  
  <pre><code>    final Handler handler = new Handler() {
      public void handleMessage(Message msg) {
  
          Integer total = msg.arg1;
          notification.contentView.setProgressBar(R.id.status_progress, 100, total, false);
          nm.notify(NOTIFICATION_ID, notification);
  
  
  
          if ( total.equals(100)  &amp;&amp; getResponse() != null ){
              // inform the progress bar of updates in progress
              uploadthread.setState(UploadThread.STATE_DONE);
  
              Intent broadcastIntent = prepareBroadcast();
              // remove the notification (we're done)
              nm.cancel(NOTIFICATION_ID);
              sendBroadcast(broadcastIntent);
          }
      }
  };
  
  
  public class UploadThread extends Thread {
  
      private String name;
      private String path;
      private String attribute;
      final static int STATE_DONE = 0;
      final static int STATE_RUNNING = 1;
      int mState;
      int total;
  
  
      public UploadThread(String fName, String path, String attribute) {
          // TODO Auto-generated constructor stub
          this.name = fName;
          this.path = path;
          this.attribute = attribute;
      }
  
      @Override
      public void run() {
          // TODO Auto-generated method stub
          setResponse(doFileUpload(name, path, attribute));
  
          if( getResponse() != null){
                              // house keeping for the application
              if(response.result.equalsIgnoreCase("success")){
                  saveFileName = response.data.savedFileName;
                  responseMessage = response.message;
              }else if (response.result.equalsIgnoreCase("error") || 
           response ==  null ){
                  responseMessage = response.message; 
                  saveFileName = null;
                  originalName = fName;
              }
          }
  
          mState = STATE_RUNNING;   
          total = 0;
          while (mState == STATE_RUNNING &amp;&amp; total &lt;=100) {
              try {
                  Log.d(TAG, "^^ Thread.sleep");
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  Logger.e("ERROR", "Thread Interrupted");
              }
              Message msg = handler.obtainMessage();
              msg.arg1 = total;
              handler.sendMessage(msg);
              total++;
          }
      }
  
      /* sets the current state for the thread,
       * used to stop the thread */
      public void setState(int state) {
          mState = state;
      }
  
  }
  </code></pre>
  
  <p>Does anyone have suggestions/ideas on how I can update progress bar to show correct upload progress?
  File Upload process:</p>
  
  <pre><code>    paramString = p.getName()+"="+URLEncoder.encode(p.getValue(), HTTP.UTF_8);
                      } catch (UnsupportedEncodingException e) {
                          Log.e(TAG, "unsupported encoding --&gt; 
             name="+p.getName()+"; value="+p.getValue(), e);
                          throw e;
                      }
                      if (combinedParams.length() &gt; 1) {
                          combinedParams += "&amp;" + paramString;
                      } else {
                          combinedParams += paramString;
                      }
                  }
              }
  
              request = new HttpPost(url + combinedParams);
  
              MultipartEntity entity = new MultipartEntity();
  
              // add headers
              for (NameValuePair h : headers) {               
                  request.addHeader(h.getName(), h.getValue());
              }
  
              // add cookies
              for(NameValuePair c : cookies) {            
                  request.addHeader(H_COOKIE,          
                           c.getName()+"="+c.getValue());                 
              }           
                   entity.addPart(uploadFile.getName(), new FileBody(uploadFile));
  
              request.setEntity(entity)
  </code></pre>