Handle Send message Mechanism _ thread

Source: Internet
Author: User
Tags stub
/** * Handle is mainly used for communication between threads, passing messages, data, and so on, the UI thread is encapsulated looper at the bottom, can be directly new handle, use handle in child threads, To call Looper.prepare (), after the instance is handle, to invoke Looper.loop (), looper to keep looping the message, Messageque carry message, handle send and receive messages, looper the equivalent conveyor,


 Messageque is the equivalent of a crate, handle equivalent to a worker.

 Using handler in the main thread, the child thread sends messages like the main thread to the public class mainactivity the extends activity {private Button btn1; Private Handler Handler = new Handler () {//From Write Handlemessage method processing work line Cheng sent messages public void Handlemessage (msg) {i
 NT str1 = MSG.ARG1;
 int str2 = MSG.ARG2;
 String str3 = (string) msg.obj;

 int STR4 = Msg.what;

 Bundle Bundle = Msg.getdata ();
 LOG.I ("==tag==", "=str1=" +str1+ "=str2=" +str2+ "=str3=" +str3+ "=str4=" +str4+ "==bundle=" +bundle.getstring ("str"));
 };
 };
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_main);

 BTN1 = (Button) Findviewbyid (R.ID.BTN1); Btn1.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO Auto-geneRated method Stub new Thread () {@Override the public void Run () {//TODO auto-generated the method stub Super.run ();
 Message msg = new Message ();//cannot new consume memory message msg = Message.obtain ();
 MSG.ARG1 = 100; MSG.ARG2 = 200;//Pass int type data Msg.obj = "sssss";//Pass Object type Msg.what = 1;//flag Current information/pass a large data Bundle Bundle = new Bundl
 E ();
 Bundle.putstring ("str", "bundle value");
 Msg.setdata (bundle);
 Handler.sendmessage (msg);//Send Message}}.start ();
 }
 }); Using Handler in a child thread, the mainline Cheng thread sends a message, and the child thread prepares a Looper public class Mainactivity extends activity {private Handler handle
 R
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main);
 //Open child thread public void start (View v) {new Mythread (). Start ();
 }//Mainline Cheng thread sends data public void Send (View v) {msg = Message.obtain ();
 Msg.what = 100;
 Msg.obj = "ASDFGHJKL";

 Handler.sendmessage (msg); \//Custom worker thread public class Mythread extends thread{@Override public void Run () {//TODO auto-generated Method Stub Super.run (); Looper.prepare ()///Create Looper object//Accept Main Line Cheng sent data handler = new handler () {@Override public void Handlemessage (msg)
 {//TODO auto-generated Method Stub super.handlemessage (msg);
 int str = msg.what;
 String str1 = (string) msg.obj;
 LOG.I ("==tag==", "==str==" +str+ "==str1==" +str1);
 }
 }; Looper.loop ()//Use loop to remove data}}

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.