title: Android: Handling onTouch events from non UI thread
body: <p>I'm not sure if what I'm trying to do make any sense but I'll give it a go:
  My phone functions as a client and I need to send data about motion events to a server.
  I'm handling the communication with an AsyncTask so I want to implement the onTouch method inside my AsyncTask class.
  I made my AsyncTask implements the View.OnTouchListener but events aren't really getting to the onTouch method inside my AsyncTask.
  I'm guessing that I'm missing something quite basic..</p>
  
  <p>Here is the AsyncTask code:</p>
  
  <pre><code>public class tpAsyncTask extends AsyncTask&lt;String,String,String&gt; implements View.OnTouchListener {
  
  Context context;
  boolean isCancelled = false;
  float prevX;
  float prevY;
  Socket socket;
  ObjectOutputStream oos;
  private final static String TAG = "ME ASYNCTASK LOGGER: ";    
  
  public tpAsyncTask(Context context){
      this.context = context;
  }
  
  @Override
  protected String doInBackground(String... strings) {
      String IP = strings[0];
      String port = strings[1];
  
      try {
          InetAddress inetAddress = InetAddress.getByName(IP);
          socket = new Socket(inetAddress,Integer.parseInt(port));
          OutputStream os = socket.getOutputStream();
          oos = new ObjectOutputStream(os);
          while(!isCancelled()) {
              Thread.sleep(1000);
          }
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
      return null;
  }
  
  @Override
  protected void onCancelled() {
      super.onCancelled();
      try {
          oos.flush();
          oos.close();
          socket.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
  // PROBLEM IS THAT THE APP NEVER REACH HERE
          if(!isCancelled()) {
              switch(motionEvent.getAction()) {
                  case MotionEvent.ACTION_MOVE:
                  {
                      Log.d(TAG, "ACTION MOVE IS PREFORMED");
                  }
                  case MotionEvent.ACTION_DOWN:
                  {
                      Log.d(TAG, "ACTION DOWN IS PREFORMED");
                  }
              }
          }
          return false;
      }}
  </code></pre>
  
  <p>Thanks</p>
  
  <p>EDIT, SOLVED:</p>
  
  <p>As many of the dudes that commented here said, I had to attach my onTouch implementation to a specific view.
  So, since I needed my onTouch events to include all of the screen's size, I simply created an object of my relative layout and passed it to the constructor of my asyncTask (that implements OnTouchListener) and on the constructor I set <code>myRelativeLayout.setOnTouchListerner(this)</code>.
  That did the trick - onTouch method inside asyncTask is now invoke whenever I touch the screen.</p>
  
  <p>Thanks a lot to everyone.   </p>