title: How to pass values from a Class to Activity - Android
body: <p>I have a newbie question about Class/Task/Activity. I'm coming from C so I don't know if it's a good approach to do what I need.</p>
  
  <p>I've created a class:</p>
  
  <pre><code>public class UDPServer {
  
      private MyDatagramReceiver myDatagramReceiver = null;
  
      private static int MAX_UDP_DATAGRAM_LEN = 1024;
      private static int UDP_SERVER_PORT = 5000;
  
      public void start() {
          myDatagramReceiver = new MyDatagramReceiver();
          myDatagramReceiver.start();
      }
  
      public void kill() {
          myDatagramReceiver.kill();
      }
  
      private class MyDatagramReceiver extends Thread {
          private boolean bKeepRunning = true;
          private String lastMessage = "";
  
          public void run() {
              String message;
              byte[] lmessage = new byte[MAX_UDP_DATAGRAM_LEN];
              DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
              DatagramSocket socket = null;
  
              try
              {
                  socket = new DatagramSocket(UDP_SERVER_PORT);
  
                  while(bKeepRunning)
                  {
                      socket.receive(packet);
                      message = new String(lmessage, 0, packet.getLength());
                      lastMessage = message;
  
                      //Here should call activity method
  
                  });
                  }
  
              }
              catch (Throwable e)
              {
                  e.printStackTrace();
              }
              finally
              {
                  if (socket != null)
                  {
                      socket.close();
                  }
              }
          }
  
          public void kill() {
              bKeepRunning = false;
          }
      }
  }
  </code></pre>
  
  <p>Inside my Activity I've:</p>
  
  <pre><code>@Override
  public void onResume() {
      super.onResume();
  
      mUDPServer = new UDPServer();
      mUDPServer.start();
  }
  
  @Override
  public void onPause() {
      super.onPause();
  
      mUDPServer.kill();
  }
  </code></pre>
  
  <p>Now, every time I received a packet I want that this thread/class pass received packet to an Activity method that elaborate(do some calculation or update some UI ecc..) this incoming data. But I can't figure how to do this, maybe my approach is not correct. I can place thread code inside Activity but it seems to make code less readable.</p>
  
  <p>Suggestion how to do this?</p>
  
  <p>@Anshul Jain CODE UPDATE:</p>
  
  <pre><code>    public class Main_activity extends Activity implements Interface_UDPServer{
      TextView recived_message;
  
      UDPServer mUDPServer;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          recived_message = (TextView)findViewById(R.id.recived_message);
      }
  
      @Override
      public void onResume() {
          super.onResume();
  
          mUDPServer = new UDPServer(this);
          mUDPServer.start();
      }
  
      @Override
      public void onPause() {
          super.onPause();
  
          mUDPServer.kill();
      }
  
      public void sendData(final String str){
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  recived_message.setText(str);
              }
          });
      }
  
  }
  </code></pre>
  
  <p>XML file</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_below="@+id/linearLayout"
          android:layout_alignLeft="@+id/linearLayout"
          android:layout_alignStart="@+id/linearLayout"&gt;
      &lt;TextView
          android:id="@+id/recived_message"
          android:layout_width="200dp"
          android:layout_height="35dp"
          android:textColor="#444444"
          android:textSize="20dp" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>