title: how to use runnable in android
body: <p>Hi, I'm newbie in Java Android development and I want to know how to use <code>Runnable</code> in Android. It doesn't seem to be working for me. Here is my source code:</p>
  
  <p><strong>MainTest.java</strong></p>
  
  <pre><code>package com.heeere.androiddnssd.discovery;
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class MainTest extends Activity {
  
      android.net.wifi.WifiManager.MulticastLock lock;
      private Discovery discovery = new Discovery(this); 
      private TextView textView;
  
      /** Called when the activity is first created. */
      @SuppressLint("NewApi") @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textView = (TextView)this.findViewById(R.id.text);
  
          android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
          lock = wifi.createMulticastLock("mylockthereturn");
          lock.setReferenceCounted(true);
          lock.acquire();
  
      }
  
      public void updateView () {
          String msg = discovery.getMsg();
          textView.setText(msg);
      }
  
      @SuppressLint("NewApi") @Override
      protected void onStop() {
          discovery.stop();
          lock.release();
          super.onStop();
      }
  
  
  }
  </code></pre>
  
  <p><strong>Discovery.java</strong></p>
  
  <pre><code>package com.heeere.androiddnssd.discovery;
  
  import java.io.IOException;
  
  import javax.jmdns.JmDNS;
  import javax.jmdns.ServiceEvent;
  import javax.jmdns.ServiceListener;
  
  public class Discovery {
  
  
      private String type = "_ikunet._tcp.local.";
      private String msg="";
      private JmDNS jmdns = null;
      private ServiceListener listener = null;
      private MainTest maintest;
      android.os.Handler handler = new android.os.Handler();
  
      public Discovery (MainTest maintest) {
          this.maintest = maintest;
          setUp();
      }
  
      private void setUp() {
  
          try {
              jmdns = JmDNS.create();
              jmdns.addServiceListener(type, listener = new ServiceListener() {
  
                  public void serviceResolved(ServiceEvent ev) {
                      msg = msg + ev.getInfo().getName()+ "\n";
                      update();
                  }
  
                  public void serviceRemoved(ServiceEvent ev) {
                  }
  
                  public void serviceAdded(ServiceEvent event) {
                      jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
                  }
              });
          } catch (IOException e) {
              e.printStackTrace();
              return;
          }
      }
  
      public String getMsg() {
          return msg;
      }
  
      private void update() {
          handler.postDelayed(new Runnable() {
              public void run() {
                  maintest.updateView();
              }
          }, 1);
      }
  
  
      public void stop() {
          if (jmdns != null) {
              if (listener != null) {
                  jmdns.removeServiceListener(type, listener);
                  listener = null;
              }
              jmdns.unregisterAllServices();
              try {
                  jmdns.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              jmdns = null;
          }
      }
  
  }
  </code></pre>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/scroller"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:fillViewport="true"&gt;
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:orientation="vertical"
                android:scrollbars="vertical"
                android:fadeScrollbars="true"
                android:isScrollContainer="true"&gt;
      &lt;TextView  
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:text="Hello World, Android Discovery" /&gt;
      &lt;TextView 
              android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" /&gt;
      &lt;Button 
              android:id="@+id/button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a Button" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p>The <code>serviceResolved</code> is executed from the <code>Discovery</code> class a while after the application starts and should update the <code>textview</code> (from <code>MainTest</code> class). But this does not happen. How do I fix this behaviour? I think it might be a <code>Runnable</code> problem.</p>