3rd Lesson 1th-Basic Knowledge Android message processing mechanism

Source: Internet
Author: User

E:\Github\hello-world\APP_Addons_0001_Message\app\src\main\java\com\thisway\app_addons_0001_message\ Mainactivity.java

Package Com.thisway.app_addons_0001_message;import Android.os.handler;import android.os.looper;import Android.os.message;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.button;import Android.view.View;import Android.util.log;import Android.os.HandlerThread; Public classMainactivity extends Appcompatactivity {PrivateButton Mbutton; PrivateFinal String tag="messagetest"; Private intButtoncount =0; PrivateThread MyThread; PrivateMyThread myThread2; PrivateHandler Mhandler; PrivateHandler MHandler3; Private intMmessagecount =0; PrivateHandlerthread myThread3; classMyrunnable implements Runnable { Public voidrun () {intCount =0;  for (;;) {LOG.D (TAG,"MyThread"+count); Count++; Try{Thread.Sleep ( the); } Catch(interruptedexception e) {e.printstacktrace (); }            }        }    }    classMyThread extends Thread {PrivateLooper Mlooper; @Override Public voidrun () {super.run ();            Looper.prepare (); Synchronized ( This) {Mlooper=Looper.mylooper ();            Notifyall ();        } looper.loop (); }         PublicLooper Getlooper () {if(!isAlive ()) {                return NULL; }            //If The thread has been started, wait until the Looper have been created.Synchronized ( This) {                 while(IsAlive () && Mlooper = =NULL) {                    Try{wait (); } Catch(Interruptedexception e) {}}} returnMlooper; }} @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mbutton=(Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {//Perform Action on ClickLOG.D (TAG,"Send Message"+buttoncount); Buttoncount++; Message msg=NewMessage ();                Mhandler.sendmessage (msg); Mhandler3.post (NewRunnable () {@Override Public voidrun () {LOG.D (TAG,"get Message for Thread3"+mmessagecount); Mmessagecount++;            }                });        }        }); MyThread=NewThread (NewMyrunnable (),"Messagetestthread");        Mythread.start (); MyThread2=NewMyThread ();        Mythread2.start (); Mhandler=NewHandler (Mythread2.getlooper (),NewHandler.callback () {@Override Publicboolean handlemessage (Message msg) {LOG.D (TAG,"Get Message"+mmessagecount); Mmessagecount++; return false;        }        }); MyThread3=NewHandlerthread ("MessageTestThread3");        Mythread3.start (); MHandler3=NewHandler (Mythread3.getlooper ()); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Publicboolean onoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        returnsuper.onoptionsitemselected (item); }}

E:\Github\hello-world\APP_Addons_0001_Message\app\src\main\res\layout\activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:paddingleft="@dimen/activity_horizontal_margin"Android:paddingright="@dimen/activity_horizontal_margin"Android:paddingtop="@dimen/activity_vertical_margin"Android:paddingbottom="@dimen/activity_vertical_margin"tools:context=". Mainactivity"android:orientation="Vertical"> <Button android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:text="Send Message"Android:id="@+id/button"android:layout_gravity="Center_horizontal"/></linearlayout>

3rd Lesson 1th-Basic Knowledge Android message processing mechanism

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.