We know that handler can send runnable messages, and he can also send messages with a message. The code is as follows
XML code
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.example.android_handler." Mainactivity " >Ten One <TextView A Android:id= "@+id/textview" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "@string/hello_world" /> - - </Relativelayout>
Java code
1 PackageCom.example.android_handler;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.os.Message;7 ImportAndroid.widget.TextView;8 9 Public classMainactivityextendsActivity {Ten PrivateTextView TextView =NULL; One PrivateHandler Handler =NewHandler () { A Public voidhandlemessage (android.os.Message msg) { -Textview.settext ("" +msg.obj); - }; the }; - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -TextView =(TextView) Findviewbyid (R.id.textview); + NewThread () { A Public voidrun () { at //This method can reclaim a message that already exists in the system -Message message =Message.obtain (); - /* - * This too - * Message message = Handler.obtainmessage - */ in - to //obj can be used to pass multiple objects +Message.obj = "123"; - handler.sendmessage (message); the }; * }.start (); $ }Panax Notoginseng}
At the same time, we know that using handler can implement the rotation of the picture in a imageview, but if we want the picture to end, the main use is handler, because handler not only can send a message, but also can remove a message. The code is as follows
XML code
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical"6Tools:context= "Com.example.android_handler1. Mainactivity ">7<ImageView8Android:id= "@+id/imageview"9Android:layout_width= "Wrap_content"Tenandroid:layout_height= "Wrap_content" One/> A<Button -Android:id= "@+id/button" -Android:layout_width= "Match_parent" theandroid:layout_height= "Wrap_content" -android:text= "@string/button_string" -/> - +</LinearLayout>
Java code
1 PackageCom.example.android_handler1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.util.Log;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.ImageView; One A Public classMainactivityextendsActivityImplementsOnclicklistener { - PrivateHandler Handler =NewHandler (); - PrivateImageView ImageView =NULL; the PrivateMyrunnable runnable =Newmyrunnable (); - PrivateButton Button =NULL; - //Create an array of pictures - Private intimages[]={r.drawable.image1, r.drawable.image2,r.drawable.image3}; + //Create an index to record the subscript of the current picture - Private intindex = 0; + @Override A protected voidonCreate (Bundle savedinstancestate) { at Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -ImageView =(ImageView) Findviewbyid (R.id.imageview); -button =(Button) Findviewbyid (R.id.button); -Button.setonclicklistener ( This); - NewThread () { in Public voidrun () { - //The first parameter is an object that passes in a runnable to //The second parameter means the time each picture is displayed +Handler.postdelayed (runnable, 1000); - }; the }.start (); * $ Panax Notoginseng } - //Create a myrunnable class to manipulate the picture on the Imagevie the classMyrunnableImplementsRunnable + { A the Public voidrun () { +index++; -index = index% 3; $ Imageview.setimageresource (Images[index]); $Handler.postdelayed (runnable, 1000); - } - the } - Public voidOnClick (View v) {Wuyi //use handler to remove runnable, to enable the carousel of Stop Pictures the handler.removecallbacks (runnable); - Wu } -}
Android handler use of the third