- Use sendEmptyMessage(int what) in Handler to send message
Click the button to send the message and process the message in the Handler. Only one message with the what attribute is sent.
case R.id.button1:
new Thread(new Runnable() {
// Check the Handler's api, which has several ways of sendMessage()
@Override
public void run() {
// Use public final boolean sendEmptyMessage (int what)
mHandler.sendEmptyMessage(3);
}
}).start();
break;
2. Use sendEmptyMessageAtTime(int what,long uptimeMillis) in Handler to send messages
case R.id.button1:
new Thread(new Runnable() {
// Check the Handler's api, which has several ways of sendMessage()
@Override
public void run() {
// Use public final boolean sendEmptyMessageAtTime (int what, long uptimeMillis)
mHandler.sendEmptyMessageAtTime(3, 1000);
}
}).start();
break;
3. Use sendEmptyMessageDelayed (int what, long delayMillis) in Handler to send messages
case R.id.button1:
new Thread(new Runnable() {
@Override
public void run() {
/*
* Use public final boolean sendEmptyMessageDelayed (int what, long delayMillis)
* Send a message with the value of what attribute, after a delay of 3 seconds
*/
mHandler.sendEmptyMessageDelayed(3, 3000);
}
}).start();
break;
4. Use sendMessage (Message msg) in Handler to send message
case R.id.button1:
new Thread(new Runnable() {
// Check the Handler's api, which has several ways of sendMessage()
@Override
public void run() {
/*
* Use public final boolean sendMessage (Message msg)
*/
// Get message object using Message.obtain()
// Message msg = Message.obtain();
// Use Handler to get message object, both can
Message msg = mHandler.obtainMessage();
msg.arg1 = 1;
msg.what = 3;
msg.obj = "AHuier";
mHandler.sendMessage(msg);
}
}).start();
break;
Combined with the above analysis, we can know that whether sending messages in android is in the form of several overloaded obtain() in Message or in the form of several overloaded sendmessages in Handler are finally sent through Handler.sendMessage, and several overloaded sendMessage() overloaded methods in Handler will finally call sendMessageAtTime() method to complete the Message queuing operation.
Reprint address: http://blog.csdn.net/ahuier/article/details/17013647