title: Quit the Looper of a Thread
body: <p>This is a more general question about how to handle the Threads and Loopers in Android, thus the code is a bit generalized. </p>
  
  <p>Consider the following class <code>DoSomethingClass</code>, which has to start some kind of action that needs listening for events (like cellular rssi changes, location changes etc).</p>
  
  <pre><code>public class DoSomethingClass {
      private Thread listenForSomethingThread;
  
      private void startDoingSomething() {
          listenForSomethingThread = new Thread(new Runnable() {
              @Override
              public void run() {
                  Looper.prepare();
                  SomethingListener listener = new SomethingListener() {
                      @Override
                      public void onSomethingHappened(Something something) {
                          // Quit looper if interrupted
                          if (Thread.currentThread().isInterrupted()) {
                              Looper.myLooper().quit();
                          } else {
                              // Do stuff with the Something if not                           
                          }
                      }
                  }
  
                  SomethingManager somMan = // Retrieve it
                  somMan.requestSomethingUpdates(listener);
                  Looper.loop();  // Blocks until Looper is quitted
                  somMan.removeSomethingUpdates(listener);                
              }
          });
          listenForSomethingThread.start();
      }
  
      private void stopDoingSomething() {
          listenForSomethingThread.interrupt();
      }
  }
  </code></pre>
  
  <p>Pretty simple: When I call <code>startDoingSomething()</code>, a new Thread gets spawned that creates a listener listening for events and handling them (eg. logging, automated yelling at callers etc). For this, it prepares and starts a looper.</p>
  
  <p>When I am finished, I call <code>stopDoingSomething()</code>, which interrupts the Thread: At the next event, nothing will be done, but the Thread will clean up and terminate.</p>
  
  <p>And here's the problem: What if that event <em>never</em> occurs? Then the check for interruption will never be called, the looper will never be quit and will loop forever!?</p>
  
  <p>Is there any way to get a Thread's Looper from another thread, in order to quit it? Or is there a possibility to let it listen for timing intervals in addition to its normal listenees?</p>