title: android define listener in a separate thread
body: <p>I'm trying to define a listener in a separate thread in my android app, however, the thread exits before that and doesn't wait for the callback functions to be invoked. </p>
  
  <p>Below is the code snippet:</p>
  
  <pre><code>new Thread(new Runnable() {
    public void run() {
      ServiceConnection conn = new ServiceConnection() {
        public void OnServiceConnected(ComponentName name, IBinder service) {
          obj = &lt;AIDL interface&gt;.Stub.asInterface(service);
        }
        public void onServiceDisconnected(ComponentName name) {
          obj = null;
        }
        Intent intent = new Intent("&lt;service name&gt;");
        context.startService(intent);
        boolean status = context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
  }).start();
  </code></pre>
  
  <p>Now when from the main thread, I start this thread and wait for onServiceConnected() to happen, it waits forever and gets never invoked. I also checked the status of this thread and it says "TERMINATED". Any idea how to fix this problem? </p>