title: Android Service, life cycle and threads
body: <p>I am working on a service, which extends the Service class and using a seperate thread to handle incoming messages, so, I have the following in my onCreate method</p>
  
  <pre><code>thread = new HandlerThread("MYSERVICE-THREAD",
          Process.THREAD_PRIORITY_BACKGROUND);
  
  thread.start();
  serviceLooper = thread.getLooper();
  serviceHandler = new ServiceHandler(mServiceLooper);
  </code></pre>
  
  <p>In this thread, I create a LocationManager and sets it up to listen for location changes. I'm slightly worried about the lifecycle of the thread though. This is why:</p>
  
  <p>If I <strong>don't</strong> remove the location listeners from the location manager in the onDestroy method of the service, and force a location update via telnet, everything works fine. I found this strange and tried explicitly ending the thread with thread.quit() in onDestroy method of the service. So, now when doing a location update from telnet, I get the warning I was kind of expecting in the first place. That a handler was calling a handler on a dead thread.</p>
  
  <p>removing the location listeners from the location manager obviously solves this, BUT, this seems to hint to me that if I dont explicitly close the thread, the thread will continue to run after the service is destroyed. Is this correct? In the examples on developer.android.com they dont explicitly close their thread, so I thought the VM would take care of it in this case.</p>