Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Change <code>connection = new Thread(new ServerThread());</code> to: <code>connection = new ServerThread();</code></p></li> <li><p>Maybe add synchronized blocks when setting/getting handler instance (it is in different threads after all :))</p></li> <li><p>Loop actually does the magic ;)</p></li> <li><p>In onDestroy quit Looper for serverThread</p></li> <li><p>And last but not least (although it does not concern Handler/Looper subject is probably the reason why you never see in logs what you expect): instead of <code>boolean onTouchEvent(MotionEvent event)</code> use <code>boolean dispatchTouchEvent(MotionEvent ev)</code> as onTouchEvent due to docs is <code>Called when a touch screen event was not handled by any of the views under it.</code> so probably this handler method is never called</p></li> </ol> <p>EDIT: Are you sure it executes sendMessage? And by the way why do you use <code>Log.v</code>? it assumes you have logging level set to verbose otherwise the logs will be abandoned. I suggest to use rather <code>Log.i</code>.</p> <p>Maybe try this code:</p> <pre><code>ServerThread connection; Handler coordHandler; public void onCreate(Bundle savedInstanceState) { connection = new ServerThread(); connection.start(); } @Override protected void onDestroy() { synchronized (this) { if(coordHandler != null) { coordHandler.getLooper().quit(); } } super.onDestroy(); } public boolean dispatchTouchEvent(MotionEvent event) { synchronized (this) { if(coordHandler == null) { Log.i(INNER_TAG, "Handler is null"); } else { Log.i(INNER_TAG, "Handler exists"); coordHandler.sendMessage( coordHandler.obtainMessage( 0, event.toString())); } } return false; } synchronized void setCoordHandler(Handler handler) { coordHandler = handler; } public class ServerThread extends Thread { public void run() { this.setName("serverThread"); Looper.prepare(); setCoordHandler(new Handler() { @Override public void handleMessage(Message msg) { Log.i(INNER_TAG, "Inside handler"); } }); Looper.loop(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload