title: How can I pass message via Handler in Android?
body: <p>I'm learning how work handler in Android. I did Android server and socket class. I want send some message (i.e. "New Connect") from socket to mainactivity, when somebody connect to server. I can't figure out how to pass from socket to mainactivity. (More in comments) </p>
  
  <p>HttpServerActivity.java</p>
  
  <pre><code>public class HttpServerActivity extends Activity implements OnClickListener{
  
  private SocketServer s;
  private static final int READ_EXTERNAL_STORAGE = 1;
  
  Button btn1, btn2;
  
  // There I'm trying to send message to button, when somebody connected
  Handler h = new Handler(){
      @Override
      public void handleMessage(Message msg){
          super.handleMessage(msg);
          String text = (String)msg.obj;
          btn1.setText(text);
      }
  };
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_http_server);
  
      Button btn1 = (Button)findViewById(R.id.button1);
      Button btn2 = (Button)findViewById(R.id.button2);
  
      btn1.setOnClickListener(this);
      btn2.setOnClickListener(this);
  
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.http_server, menu);
      return true;
  }
  
  
  @Override
  public void onClick(View v) {
  
      if (v.getId() == R.id.button1) {
  
          int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
  
          if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
              ActivityCompat.requestPermissions(
                      this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
          } else {
              // I dont know figure out in this place 
              s = new SocketServer(h);
              s.start();
          }
      }
      if (v.getId() == R.id.button2) {
          s.close();
          try {
              s.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  
  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      switch (requestCode) {
  
          case READ_EXTERNAL_STORAGE:
              if ((grantResults.length &gt; 0) &amp;&amp; (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                  // I dont know figure out in this place 
                  s = new SocketServer(h);
                  s.start();
              }
              break;
  
          default:
              break;
      }
  }
  }
  </code></pre>
  
  <p>SocketServer.java</p>
  
  <pre><code>public class SocketServer extends Thread {
  
  private final Handler mHandler;
  public SocketServer(Handler handler)
  {
      mHandler = handler;
  }
  ServerSocket serverSocket;
  public final int port = 12345;
  boolean bRunning;
  public void close() {
      try {
          serverSocket.close();
      } catch (IOException e) {
          Log.d("SERVER", "Error, probably interrupted in accept(), see log");
          e.printStackTrace();
      }
      bRunning = false;
  }
  
  public Handler mHandler;
  public void run() {
      try {
          Log.d("SERVER", "Creating Socket");
          serverSocket = new ServerSocket(port);
          bRunning = true;
          while (bRunning) {
              Log.d("SERVER", "Socket Waiting for connection");
              Socket s = serverSocket.accept();
              Log.d("SERVER", "Socket Accepted");
  
              // trying to send some message
              String[] messageString = new String[1];
  
              Message message = Message.obtain();
              messageString[0]="OK";
              message.obj = messageString;
  
              mHandler.sendMessage(message);
  
              OutputStream o = s.getOutputStream();
              BufferedWriter out = new BufferedWriter(new OutputStreamWriter(o));
  
              out.write("HTTP/1.0 200 OK\n" +
                      "Date: Fri, 31 Dec 1999 23:59:59 GMT\n" +
                      "Content-Type: text/html\n" +
                      "Content-Length: 1354\n" +
                      "\n" +
                      "&lt;html&gt;\n" +
                      "&lt;body&gt;\n" +
                      "&lt;h1&gt; Connected &lt;/h1&gt;\n" +
                      "&lt;/body&gt;\n" +
                      "&lt;/html&gt;");
              out.flush();
  
  
              while (!((tmp = in.readLine()).isEmpty()))
              {
                  Log.d("Header", tmp);
                  if (tmp.startsWith("GET"))
                  {
                      getRequest = tmp;
                  }
              }
  
              s.close();
              Log.d("SERVER", "Socket Closed");
  
          }
      }
      catch (IOException e) {
          if (serverSocket != null &amp;&amp; serverSocket.isClosed())
              Log.d("SERVER", "Normal exit");
          else {
              Log.d("SERVER", "Error");
              e.printStackTrace();
          }
      }
      finally {
          serverSocket = null;
          bRunning = false;
      }
  }
  
  }
  </code></pre>