title: Update ProgressDialog from Thread inside Service
body: <p>i want to increment a progress dialog from a thread inside a service, i have really hard time doing that, this is my code please help me.</p>
  
  <p>I tried many different ways including asyncTask (I had problem with context)
  and tried with static functions but its not working properly,
  I pretty new with android please explain me the problem here.</p>
  
  <p>the activity</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  ProgressDialog progressBar;
  
  private void showProgrssBar() {
  
      progressBar.show();
  }
  
  private void dismissProgressBar() {
  
      progressBar.dismiss();
  }
  
  private void increaseProgressBar(int total) {
  
      progressBar.incrementProgressBy(total);
  };
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
       createProgressBarDialog();
  
  Intent n = new Intent(this, myService.class);
  startService(n);
  
  }
  
  private void createProgressBarDialog()
  {
      progressBar = new ProgressDialog(this);
      progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progressBar.setMax(200);
      progressBar.setMessage("Recieving bluetooth data");
      progressBar.setCanceledOnTouchOutside(false);
  }
  </code></pre>
  
  <p>the service:</p>
  
  <pre><code> public class myService extends Service
  {
  
      private myThread myThread;
  
      Handler handler = new Handler()
      {
          @Override
          public void handleMessage(android.os.Message msg) 
          {
              int total = msg.getData().getInt("total");
  
              if (total == -1)
              {
                  dismissProgressBar();
              }
              else if (total == 0)
              {
                  showProgrssBar();
              }
              else
              {
                  increaseProgressBar(total);
              }       
          }   
      };
  
              @Override
              public int onStartCommand(Intent intent, int flags, int startId)
              {
                  myThread = new myThread(handler);
                  myThread.start();
  
                  return START_STICKY;
              }
  </code></pre>
  
  <p>the thread</p>
  
  <pre><code>  class myThread extends Thread
      { 
           Handler h;
      int numOfLinesToRead = 220;
      int line = 0;
  
  
      public myThread(Handler h)
      {
          this.h = h;
      }
  
      private void increaseProgressBarOnActivity(int i_MsgType)
      {
           Message msg = h.obtainMessage();
           Bundle b = new Bundle();
           b.putInt("total", i_MsgType);
           msg.setData(b);
           h.sendMessage(msg);
      }
  
      @Override
      public void run() {
          super.run();
          int increase;
          try
          {
              Thread.sleep(1);
          }
          catch (InterruptedException e)
          {
  
              e.printStackTrace();
          }
            for (; line &lt; 220; line++) 
              {
                 increase = (line*100/numOfLinesToRead);
                  if (increase != 0)
                  {
                      increaseProgressBarOnActivity(increase);   
                      try
                      {
                          Thread.sleep(90);
                      }
                      catch (InterruptedException e)
                      {
  
                          e.printStackTrace();
                      }
  
                  }
              }
  
      }
  }
  </code></pre>