Obtainmessage () in handler in Android

Source: Internet
Author: User


Public final Message obtainmessage (int, int arg1, int arg2, Object obj)
Here are the 4 parameters to explain ah ...

byte[] buffer = new byte[1024];
int bytes;
bytes = mminstream.read (buffer);
Mhandler.obtainmessage (Mainactivity.message_read, Bytes,-1, buffer). Sendtotarget ();

This is the code for the instance.

Case Message_read:

Byte[] Readbuf = (byte[]) msg.obj;//is this a buffer given to READBUF?

string ss = new String (readbuf, 0,MSG.ARG1);//What does this sentence mean, it seems that the arg1 is the value read in



--------------------------------------------------------------------------------------
-is the value of your message passed to handler,

Message msg = Handler.obtainmessage ();
MSG.ARG1 = 1;
MSG.ARG2 = 2;
Msg.what = 3;
Msg.obj = Object;

Receive in Handler
Handler Handler = new Handler () {

@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Receive messages sent by message msg.what msg.arg1 msg.arg2 msg.obj
Here, msg.obj need to be strong into the data type you passed.
}

};
-------------------------------------------------------
Mhandler.obtainmessage (Mainactivity.message_read, Bytes,-1, buffer). Sendtotarget ();
-------------------------------------------------------
New String (Readbuf, 0,MSG.ARG1); String (byte[] data, int offset, int byteCount)
MSG.ARG1 acts as a bytecount, which is the character length. MSG.ARG1 is not the assignment of bytes before the message, and bytes = mminstream.read (buffer), which is to give the single character bytes






Obtainmessage () in handler in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.