title: Sending a simple message from Service to Activity
body: <p>I am trying to find a way to send an integer value from my Service to my MainActivity, yet I can't figure it out with the tutorials on the internet. I am using a Handler to receive my messages which I'm sending from a Thread in my Service.</p>
  
  <p>My code for the Thread in the service:</p>
  
  <pre><code>private class ServiceThread extends Thread {
      @Override
      public void run() {
          super.run();
          try{
              Log.i("Service", "TASK PERFORMED");
  
              int x = 0;
              for (int i = 0; i &lt; 20; i++){
                  x += (random.nextInt(20) + 20);
              }
              theInteger = x/20;
              Message msg = Message.obtain();
              msg.what = MY_RND_INT;
              msg.arg1 = theInteger;
              handler.sendMessage(msg);
          }catch(Exception e){
              e.getMessage();
          }
      }
  }
  </code></pre>
  
  <p>My code for attempting to receive the message:</p>
  
  <pre><code>public Messenger mMessenger = new Messenger(new IncomingHandler());
  
  
  class IncomingHandler extends Handler {
      @Override
      public void handleMessage(Message msg) {
          switch (msg.what) {
              case SimpleService.MY_RND_INT:
                  rndInt.setText(msg.arg1);
                  break;
              default:
                  super.handleMessage(msg);
          }
      }
  }
  </code></pre>