package com.handlerthread;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.HandlerThread;
Import Android.os.Looper;
Import Android.os.Message; public class HandlerThread2 extends activity {@Override protected void onCreate (Bundle savedinstancestate) {SUPER.O
Ncreate (savedinstancestate);
Setcontentview (R.layout.main);
SYSTEM.OUT.PRINTLN ("Activity thread ID:" +thread.currentthread (). GetId ());
Handlerthread handlerthread = new Handlerthread ("Handlerthread");
Handlerthread.start ();
MyHandler handler = new MyHandler (Handlerthread.getlooper ());
Message msg = Handler.obtainmessage ();
MSG.ARG1 = 123;//Pass integer data//msg.obj = "ASD"; Pass object type///Use Bundle object to pass value Bundle B = new Bundle ();
B.putint ("ID", 12);
B.putstring ("name", "ThinkPad");
Msg.setdata (b);
Msg.sendtotarget ();
Class MyHandler extends Handler {public MyHandler () {super (); } public MyHandler (Looper loopeR) {super (Looper);
@Override public void Handlemessage (msg) {super.handlemessage);
int args = MSG.ARG1;
string s = (string) msg.obj;
Gets the value of the Bundle object Bundle b = Msg.getdata ();
int id = b.getint ("id");
String name = b.getstring ("name");
SYSTEM.OUT.PRINTLN ("ID is" +id+ ", the name is" +name);
System.out.println ("Handler thread ID:" +thread.currentthread (). GetId ()); }
}
}
Run results
So, what exactly is bundle? Let's take a look at the official explanation.