Updating the UI with handler
1 PackageActivity.cyq.handlermessage;2 3 Importandroid.content.res.Resources;4 ImportAndroid.graphics.Bitmap;5 Importandroid.graphics.BitmapFactory;6 ImportAndroid.os.Handler;7 ImportAndroid.os.Message;8 Importandroid.support.v7.app.AppCompatActivity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.ImageView; A ImportAndroid.widget.TextView; - ImportAndroid.widget.Toast; - the - Public classMainactivityextendsappcompatactivity { - PrivateTextView TextView; - Privatebutton button; + PrivateImageView ImageView; - + Private int[] Iamgeid =New int[]{r.drawable.aaa1, R.drawable.aaa3, r.drawable.image1}; A at - PrivateHandler Handler =NewHandler (NewHandler.callback () { - @Override - Public Booleanhandlemessage (Message msg) { -Toast.maketext (mainactivity. This, "This is handler's return function.", Toast.length_short). Show (); - /*If True then terminates execution of Handler (), which can be used to judge the contents of the message and decide whether to continue processing the messsage*/ in return false; - } to }) { + @Override - Public voidhandlemessage (Message msg) { the Super. Handlemessage (msg); *Textview.settext (Msg.arg1 + "" +msg.arg2); $ Imageview.setimagebitmap ((Bitmap) msg.obj);Panax Notoginseng } - }; the + @Override A protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); - $TextView =(TextView) Findviewbyid (R.id.textview); $button =(Button) Findviewbyid (R.id.button); -ImageView =(ImageView) Findviewbyid (R.id.imageview); - /*Root UI via Handler.post (Runnable) method*/ the NewThread () { - @OverrideWuyi Public voidrun () { the Super. Run (); - /*Create a Bitmap object*/ WuResources res =getresources (); -Bitmap BMP =Bitmapfactory.decoderesource (res, R.DRAWABLE.AAA1); About /*Send Message*/ $Message message =NewMessage (); -MESSAGE.ARG1 = 123; -MESSAGE.ARG2 = 245; -Message.obj =bmp; A /*arg1 arg2 is an int type data obj is an object type data*/ + handler.sendmessage (message); the } - }.start (); $ the the } the the -}
Handler Send Message