Package com. zzyz. example;
Import java. util. timer;
Import java. util. timertask;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. logoff;
Import Android. OS. message;
Import Android. util. log;
Public class handletest extends activity {
Private Static final string tag = "mainthread ";
Private timer;
Private timertask;
Private handler mmainhandle, mchildhandle;
Private int I = 0;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Timer = new timer ();
Timertask = new mytimertask ();
Mmainhandle = new handler (){
Public void handlemessage (Message MSG ){
Log. E ("mainhandle", I + "");
System. Out. println ("receive a message! ");
}
};
New childthread (). Start ();
Timer. Schedule (timertask, 0, 5000 );
}
Public class mytimertask extends timertask {
Public void run (){
If (mchildhandle! = NULL ){
Message childmsg = mchildhandle. obtainmessage ();
Mchildhandle. sendmessage (childmsg );
Log. E ("Thread", I + "");
}
}
}
Public void ondestroy (){
Log. I (TAG, "stop looping the child threads message queue ");
Mchildhandle. getlooper (). Quit ();
}
Class childthread extends thread {
Public void run (){
Logoff. Prepare ();
Mchildhandle = new handler (){
Public void handlemessage (Message MSG ){
Log. I ("child", "11111111 ");
I ++;
Message tomain = mmainhandle. obtainmessage ();
Tomain. OBJ = This. getlooper (). getthread (). getname () + I;
Mmainhandle. sendmessage (tomain );
System. Out. println ("send a message! ");
Log. I ("I =", I + "");
}
};
Logoff. Loop ();
}
}
}