The android.os.Handler.obtainMessage() method is used to obtain a new Message object which can then be passed to the Handler.sendMessage() method. This method takes no arguments and returns a new Message object that is initialized with default values.
Examples:
Example 1:
Handler handler = new Handler(); Message msg = handler.obtainMessage(); msg.what = 1; // set message code msg.obj = "Hello World"; // set message data handler.sendMessage(msg);
This code creates a new Handler object and obtains a new Message object using the obtainMessage() method. The message code is set to 1 and the message data is set to "Hello World". Finally, the message is sent using the sendMessage() method.
Example 2:
Handler handler = new Handler(); for (int i = 0; i < 10; i++) { Message msg = handler.obtainMessage(); msg.what = i; // set message code handler.sendMessage(msg); }
This code creates a new Handler object and sends 10 messages using a for loop. Each message has a different message code between 0 and 9.
The android.os.Handler.obtainMessage() method belongs to the android.os package library.
Java Handler.obtainMessage - 30 examples found. These are the top rated real world Java examples of android.os.Handler.obtainMessage extracted from open source projects. You can rate examples to help us improve the quality of examples.