Andorid Multithreading Handler Usage

Source: Internet
Author: User

. xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://"Xmlns:tools= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.hanqi.application3.UIActivity7"android:orientation= "vertical">    <AutocompletetextviewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "AutoComplete text edit box"Android:id= "@+id/actv_1"Android:completionthreshold= "1"/>    <!--Completionthreshold hints for required characters -    <SpinnerAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/sp_1">    </Spinner>    <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "status bar Prompt"Android:onclick= "Notification_onclick"/>    <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/da2"Android:id= "@+id/iv_3"/></LinearLayout>

. java

 PackageCom.hanqi.application3;;;;Importandroid.content.Intent;ImportAndroid.os.Handler;ImportAndroid.os.Message;;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.AutoCompleteTextView;ImportAndroid.widget.ImageView;ImportAndroid.widget.Spinner;ImportAndroid.widget.Toast;ImportJava.util.Timer;ImportJava.util.TimerTask; Public classUIActivity7extendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_UI7); Autocompletetextview actv_1=(Autocompletetextview) Findviewbyid (; //set up an auto-complete data sourcestring[] Strings= {' China ', ' child ', ' Chile ', ' Chinese ', ' Add ', ' All ', ' Class ', ' DDP '}; Arrayadapter<String> Arrayadapter =NewArrayadapter<string> ( This, r.layout.layout_array,strings);        Actv_1.setadapter (Arrayadapter); //Drop- down listString[] strings1 = {' China ', ' child ', ' Chile ', ' Chinese ', ' Add ', ' All ', ' Class ', ' DDP '}; Arrayadapter<String> ArrayAdapter1 =NewArrayadapter<string> ( This, R.LAYOUT.LAYOUT_ARRAY,STRINGS1); Spinner SP1=(Spinner) Findviewbyid (;        Sp1.setadapter (ArrayAdapter1); Sp1.setonitemselectedlistener (NewAdapterview.onitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?> Parent, view view,intPositionLongID) {toast.maketext (UIActivity7. This, "position =" +position, Toast.length_short). Show (); } @Override Public voidOnnothingselected (adapterview<?>parent) {Toast.maketext (UIActivity7). This, "No choice", Toast.length_short). Show ();        }        }); //Handler        FinalImageView Iv3 =(ImageView) Findviewbyid (; Final int[] Iimageid ={R.DRAWABLE.DA1,R.DRAWABLE.DA2,R.DRAWABLE.DA3,R.DRAWABLE.DA4}; //Inherit Handler        FinalHandler Handler =NewHandler () {inti = 0; //callback method for handling messages//rewrite@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); //determine the category of a message                if(Msg.what ==1)                {                    //Toggle PictureIv3.setimageresource (iimageid[i++%iimageid.length]);//i++;//if (i==10)//                    {//i=0;//                    }                }            }        }; //send a message on a new thread//Fixed loop send//Timer: Start a new thread, execute code at timed time        NewTimer (). Schedule (NewTimerTask () {@Override Public voidrun () {//Send Message//Empty MessageHandler.sendemptymessage (1); }        },1000,2000); }     Public voidNotification_onclick (View v) {//1. Get status Bar message managerNotificationmanager nm =(Notificationmanager) Getsystemservice (Notification_service); //3. Build IntentionsIntent Intent =NewIntent ( This, UIActivity2.class); //4. Get PendingintentPendingintent pendingintent = pendingintent.getactivity ( This, 0,intent,0); //2. Build a message method chain callNotification NT =NewNotification.builder ( This). Setcontenttitle ("This is a notice"). Setcontenttext ("This is the notification content: Click to open the new interface"). Setticker ("New Notice"). Setsmallicon (R.DRAWABLE.DA1)//Image. Setautocancel (true). SetDefaults (Notification.default_all)//Sound. Setcontentintent (pendingintent). build (); //give the manager a messageNm.notify (0, NT); }}

Andorid Multithreading Handler Usage

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: 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.