Android Learning Note--bundle

Source: Internet
Author: User

The function of this project is to realize the transfer of data between different threads.

The following code is the code in

 PackageCom.example.bundle;;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.HandlerThread;ImportAndroid.os.Looper;ImportAndroid.os.Message;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Prints the ID of the current threadSystem.out.println ("activity-->" +Thread.CurrentThread (). GetId ()); //generates a Handlethread object that implements the function of using looper to handle Message Queuing, which is provided by the Android program FrameworkHandlerthread Handlerthread =NewHandlerthread ("Handler_thread"); //before using the Handlerthread Getlooper () method, you must first call the class's start ()Handlerthread.start (); MyHandler MyHandler=NewMyHandler (Handlerthread.getlooper ()); Message msg=Myhandler.obtainmessage (); //msg.obj= "ABC"; //sends MSG to the target object, the so-called target object, which is the handler object that generated the MSG objectBundle B =NewBundle (); B.putint ("Age", 20); B.putstring ("Name", "John");        Msg.setdata (b);    Msg.sendtotarget (); }        classMyHandlerextendshandler{ PublicMyHandler () {} PublicMyHandler (Looper Looper) {//Super Call parent class            Super(Looper); }         Public voidhandlemessage (Message msg) {//string s = (string) msg.obj;Bundle B =Msg.getdata (); intAge = B.getint ("Age"); String name= b.getstring ("name"); System.out.println (' Age ' + Age + ', name is ' +name); System.out.println ("Handler-->" +Thread.CurrentThread (). GetId ()); System.out.println ("Handlermessage"); }    }}

The following code is the code in Activity_main.xml

<linearlayout xmlns:android= ""    xmlns:tools= "http// "    android:id=" @+id/linearlayout1 "    android:layout_width=" Match_parent "    android:layout_height=" Match_parent "    android:orientation=" vertical "     Tools:context= "${relativepackage}.${activityclass}" >    <TextView        android: Layout_width= "Wrap_content"        android:layout_height= "Wrap_content"        android:text = "@string/hello_world"/></linearlayout>

Android Learning Note--bundle

Related Article

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.