title: How can I (should I) add a handler to a thread in a Service
body: <p>I'm still working on a short Service example (page 304) of <a href="http://www.apress.com/9781430226598" rel="nofollow">Pro Android 2</a> Again, the Service example that consists of two classes: BackgroundService.java shown below and MainActivity.java shown below. Now I want to extend this code to pass data to another activity in my application. From what I've learned I added the beginnings of a handler to the code below:</p>
  
  <pre><code>    public class MainActivity extends Activity {
          private static final String TAG = "MainActivity";
  
          @Override
          public void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
  
              Log.d(TAG, "starting service");
  
              Button bindBtn = (Button)findViewById(R.id.bindBtn);
              bindBtn.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View arg0) {
                      Intent backgroundService = new Intent(MainActivity.this, com.marie.mainactivity.BackgroundService.class);
                      startService(backgroundService);
                  }
              });
  
              Button unbindBtn = (Button)findViewById(R.id.unbindBtn);
              unbindBtn.setOnClickListener(new OnClickListener() {
                  @Override
                  public void onClick(View arg0) {
                      stopService(new Intent(MainActivity.this, BackgroundService.class));
                  }
              });
          }
      }
  
      // The handler code I added
      // I'm not sure what fills out the msg.what field for the switch
      private Handler messageHandler = new Handler() {
  
          @Override
          public void handleMessage(Message msg) {  
              switch(msg.what) {
                  //handle update
                  //possibly update another activity???
              }
          }
  
      };
  
      public class BackgroundService extends Service {
          private NotificationManager notificationMgr;
  
          @Override
          public void onCreate() {
              super.onCreate();
  
              notificationMgr = NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  
              displayNotificationMessage("starting Background Service");
  
              Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService");
              thr.start();
          }   
  
          class ServiceWorker implements Runnable
          {
              public void run() {
                  mResult = doSomethingTimeConsuming();
  
                  //Use the handler to send update to the main thread or another activity???
                  messageHandler.sendMessage(Message.obtain(messageHandler, mResults));
  
                  BackgroundService.this.stopSelf();
              }
          }
  
          @Override
          public void onDestroy()
          {
              displayNotificationMessage("stopping Background Service");
              super.onDestroy();
          }
  
          @Override
          public void onStart(Intent intent, int startId) {
              super.onStart(intent, startId);
          }
  
          @Override
          public IBinder onBind(Intent intent) {
              return null;
          }
  
          private void displayNotificationMessage(String message)
          {
              Notification notification = new Notification(R.drawable.note, message, System.currentTimeMillis());
  
              PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
  
              notification.setLatestEventInfo(this, "Background Service", message, contentIntent);
  
              notificationMgr.notify(R.id.app_notification_id, notification);
          }
      }
  </code></pre>
  
  <p>I've learned that when I get the Service instance I can pass it a Handler. But I don't know how to do that. </p>