The function of this project is to realize the transfer of data between different threads.
The following code is the code in Mainactivity.java
PackageCom.example.bundle;Importandroid.app.Activity;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= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " 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