title: Android: how to communicate from worker thread to a service
body: <p>I've created a service class and a worker class that is executed in a separate thread. I would like to setup a communication between them, so the worker could send some status back to the service.</p>
  
  <p>I've tried to convert my worker <code>Thread</code> to <code>HandlerThread</code> and setup a <code>Handler</code> on the service side, but then I don't know how to actually send a message from the worker. It looks like I can't grasp the concept.</p>
  
  <p>Here's my classes (without communication logic):</p>
  
  <h3>Service class</h3>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.app;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.Log;
  
  
  public class ConnectionService extends Service {
  
      protected ConnectionWorker thread;
  
      @Override
      public void onCreate() {
  
          super.onCreate();
  
          // Creating a connection worker thread instance.
          // Not starting it yet.
          this.thread = new ConnectionWorker();
  
          Log.d(this.getClass().getName(), "Service created");
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          Log.d(this.getClass().getName(), "Service started");
  
          // Checking if worker thread is already running.
          if (!this.thread.isAlive()) {
  
              Log.d(this.getClass().getName(), "Starting working thread");
  
              // Starting the worker thread.
              this.thread.start();
          }
  
          return Service.START_STICKY;
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onDestroy() {
  
          Log.d(this.getClass().getName(), "Stopping thread");
  
          // Stopping the thread.
          this.thread.interrupt();
  
          Log.d(this.getClass().getName(), "Stopping service");
  
          super.onDestroy();
  
          Log.d(this.getClass().getName(), "Service destroyed");
      }
  }
  </code></pre>
  
  <h3>Worker class</h3>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.app;
  
  import android.os.SystemClock;
  import android.util.Log;
  
  
  public class ConnectionWorker extends Thread {
  
      public ConnectionWorker() {
          super(ConnectionWorker.class.getName());
      }
  
      @Override
      public void run() {
  
          super.run();
  
          Log.d(this.getClass().getName(), "Thread started");
  
          // Doing the work indefinitely.
          while (true) {
  
              if (this.isInterrupted()) {
                  // Terminating this method when thread is interrupted.
                  return;
              }
  
              // @todo: send a message to the service
  
              Log.d(this.getClass().getName(), "Doing some work for 3 seconds...");
              SystemClock.sleep(3 * 1000);
          }
      }
  }
  </code></pre>
  
  <p>How do I implement a <code>HandlerThread</code>, send messages from the worker thread and receive them in my service?</p>
  
  <p>I will highly appreciate a code example.</p>
  
  <h1>Update</h1>
  
  <p>Actually, it looks like I can't use <code>Looper</code> inside of my thread cause it will block the thread execution and I don't want that. Is it possible to send messages from the thread without using <code>Looper</code>?</p>