The difference between Handler.dispatchMessage(msg) and Handler.sendMessage(msg)

Related searches

When I use Handler.dispatchMessage(msg), the handleMessage(Message msg) will be run on new thread but when I use Handler.sendMessage(msg), the handleMessage(Message msg) will be run on main thread. Who can tell me the difference between them?



public class MainActivity extends Activity
private String TAG = "MainActivity";
private Handler mHandler = new Handler()
    public void handleMessage(Message msg)
        Log.i(TAG, "Handler:" + Thread.currentThread().getId() + " & arg1=" + msg.arg1);


public void onCreate(Bundle savedInstanceState)
    Log.i(TAG, "Main:" + Thread.currentThread().getId());

private void testMethod()
    Thread thread = new Thread()
        public void run()
            Log.i(TAG, "Thread:" + Thread.currentThread().getId());
            Message msg = mHandler.obtainMessage();
            msg.arg1 = 1;
            Message msg2 = mHandler.obtainMessage();
            msg2.arg1 = 2;




04-19 11:32:10.452: INFO/MainActivity(774): Main:1
04-19 11:32:10.488: INFO/MainActivity(774): Thread:8
04-19 11:32:10.492: INFO/MainActivity(774): Handler:8 & arg1=1
04-19 11:32:10.635: INFO/MainActivity(774): Handler:1 & arg1=2

mHandler.dispatchMessage(msg) is like directly calling handleMessage(Message msg) and I don't know when that would be useful. The point of Handlers is the ability to send messages to other threads. That's what you do with sendMessage.

Edit: as you can see it just calls handleMessage() for you.

 * Handle system messages here.
public void dispatchMessage(Message msg) {
    if (msg.callback != null) {
        // callback = the Runnable you can post "instead of" Messages.;
    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {

Handler, The message is enqueued by the Handler on the worker thread and processed by the Handler on the UI thread. message: what — an identifier the Handler can use to distinguish messages and process them differently public final Message obtainMessage(int what, Object obj) dispatchMessage(msg); Message: It is a class that defines various useful methods to deal with message data. To send an object we set the obj variable. Message msg = new Message(); msg.obj = "Ali send message"; handler.sendMessage(msg);

The messages sent with Handler.sendMessage() will be handled on the thread you created in testMethod().

The messages sent with Handler.dispatchMessage() are handled on the main thread.

Android Handler Internals. For an Android application to be…, As a quick example of how to use a Thread with a basic Handler in an public void handleMessage(Message msg) { Bundle bundle = msg. Please note that this source code is heavily based on the now-old book, Android 4.4 App Development. I was just looking into threads and handlers in Android last night, and this is a relatively simple example of how to send a message from a thread/runnable to a handler. Note that I had to add the @SuppressLint annotation to this code. I don’t

If You Call the Handler.dispatchMessage() in the Main Thread Then The Message is processed in Main Thread.

If You Call The Handler.dispatchMessage() in the Worker Thread Then The Message is Processed in Worker Thread.

When You Call Handler.sendMessage(msg) The Message is Processed in the Thread Which Created The Handler.

Android: How to send a message from a Thread to a Handler , Handler is the primary handler of a Message and handles messages handleMessage(msg); //Get the Message object you just sent and do Processing messages in dispatchMessage, where the runnable in the post method is msg. Handler handles messages with a strict distinction between whether it� Type: const MSG* A pointer to a structure that contains the message. Return value. Type: LRESULT. The return value specifies the value returned by the window procedure. Although its meaning depends on the message being dispatched, the return value generally is ignored. Remarks. The MSG structure must contain valid

Android Component Handler Looper Message Understanding, You can suppress that particular message. When the job runs it will look in the local handler (if one exists) for each message to see if any rules exist for that message type. If a particular message is not handled locally, it will look to the project-wide handler for rules. If there are none there, it writes the message to the job log.

关于android:Handler.dispatchMessage(msg)和Handler , Message Message Remarks Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Why we use handlers with thread : When we install an application in android then it create a thread for that application called MAIN UI Thread, All activities run inside that thread , By the android single thread model rule we can not access UI elements (bitmap , textview etc..) directly for another thread defined inside that activity.

  • @Waqas from your code mHandler.dispatchMessage(msg) - it's the sourcecode for Handler.dispatchMessage (slightly simplified)
  • yes but dispatchMessage(Message msg) says msg, but in code its also using message which is whom i'm concerning about
  • oops, my fault, I've inlined some code there and failed to correct the name.
  • I think you mis-read the question and wrote it in the wrong way. sendMessage's messages handled in main thread and dispatchMessage's messages handled in the newly created message. You are also not answering to the question of Why?