android - Handlers initialized with Looper.getMainLooper() does not respond to message callbacks -


I am trying to implement the hearing on the same loop with different threads.

Below I have two headlars, one is built in the main thread, the other is in the thread, though both have been initialized to listen on the main loop.

  Private handler mMain; Public Static Final Threadpool Excl. Tape = (Threadpool Exchanger) Exporter. Newcastle Threadpool (); Override public null on @reate (bundle saved instainstate) {super.naught (savedinstenstate); SetContentView (R.layout.activity_main); MMain = new handler (Looper.getMainLooper ()) {@Override public void handleMessage (message msg) {log. Wtf ("", "main:" + msg); }}; Tpe.execute {New handhelder tChild = new handler (Looper.getMainLooper ()) {@Override Public void handleMessage (Message msg) {Log. Wtf ("", "child:" + msg);}} ; @ Override Public Wide Run () {Log. Wtf ("," Send msg to Main Lauper); tChild.sendEmptyMessage (100);}}); }   

But when I send a message below, only the child handler prints the message. The main handler does not receive the message. 03-20 22: 02: 26.754: A / (12857): Send a message to the main loop 03-20 22: 02: 26.847: A / (12857): Children: {what = 100 when = -8ms}

What am I doing wrong? Thanks for reading.

controls every handler example message There is no way to target and share them, so every message or post is sent to handler which is executed by that example.

Luper indicates that which thread will send messages / runnabels sent out. In your code, both threaders will execute handleMessage () on the main thread, even though they are created on separate threads. This is the real reason that you can send a luper example to handler if you do not pass any Looper , then < Strong> then handler will execute the code on the thread in which it was created (which should also be should also be a Looper thread).

Besides, for this reason there is no reason to create many handlers to post data in such a way. A single handler is designed to send messages from multiple threads, and they are all sorted in MessageQueue and executed on selected Looper threads Are there. You can post it directly from the background thread on mMain to execute the code on that thread. In this case, going under the Looper , the code is redundant on the main thread already.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -