赞
踩
XiXiSystem: 该系统类一旦启动,将每个一定的时间去调用TCPCommunication中的方法
start(){
handler.post(update);
}
Runnable update = new Runnable(){
public void run(){
XiXiSystem.handle();
handler.postDelayed(update,500);
}
};
handle(){
communication.handleNMessage(); //调用TCPCommucation中的方法,来读取从服务器返回的数据内容。
}
TCPCommunication:
该TCP通道一旦建立,它将启动一条发送数据的线程。
此方法读取服务器返回的数据,然后将数据通过Handler消息机制,发送给当前的
MainHandler,也就是当前Activity中的handler.
handleNMessage(){
reader = inQueue.elementAt(0);
Message msg = new Message();
msg.what = msgType;
msg.obj=reader; XiXiSystem.mainhandler.
sendMessage(msg);
}
MainActivity:
//当前运行的Activity中的Handler, 拿到服务器返回的数据后,将调用execute(msg)来处理数据,并将数据显示在界面上。
Handler handler = new Handler(){
public void handleMessage(Message msg){
activity.execute(msg);
}
};
XiXiSystem.mainhandler = handler;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。