我已经在网上搜索了一些明确的例子,但到目前为止找不到我可以应用于我的项目。
我正在尝试创建一个每100毫秒运行一次的工作线程。然后它应该用结果更新UI。经过一些研究后,我决定使用Handler来管理UI更新。我来到这个解决方案:
我的活动的处理程序:
private final Handler handler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Log.v("udppacket", "UDP message!");
if (msg.obj != null)
{
ManagerUdpPacket p = (ManagerUdpPacket) msg.obj;
operatorListFragment.updateContent((int) p.getOperationTime());
}
}
else
{
}
}
};
我的另一个有工作线程的类:
public class ManagerUdpReceiver
{
private int minPort = 1234;
private int maxPort = 1240;
private ArrayList<PortListener> portList;
private Handler handler;
private Thread portThread;
private int queryInterval = 100;
private boolean stop = false;
public ManagerUdpReceiver(int minport, int maxport, Handler handler, int queryInterval)
{
minPort = minport;
maxPort = maxport;
this.handler = handler;
this.queryInterval = queryInterval;
//create port listeners from given range and start their threads
start();
}
private void start()
{
stop = false;
// Create Inner Thread Class
portThread = new Thread(new Runnable()
{
// After call for background.start this run method call
public void run()
{
if (portList == null)
{
portList = new ArrayList<PortListener>();
for (int i = minPort; i < maxPort; i++)
{
portList.add(new PortListener(i));
}
}
if (!stop)
{
ManagerUdpPacket p = portList.get(0).receive();
threadMsg("moi", p);
//mHandler.postDelayed(this, queryInterval);
}
else
{
//stop execution and close ports
for (int i = 0; i < portList.size(); i++)
{
portList.get(i).close();
}
}
}
//send message to the handler
private void threadMsg(String msg, ManagerUdpPacket p)
{
if (!msg.equals(null) && !msg.equals(""))
{
Message msgObj = handler.obtainMessage();
//msgObj.obj = p;
Bundle b = new Bundle();
b.putString("message", msg);
msgObj.setData(b);
handler.sendMessage(msgObj);
}
}
});
// Start Thread
portThread.start();
}
public void close()
{
stop = true;
}
}
当我运行程序时,我在UI线程中运行网络代码时遇到异常。现在,工作线程应该接收并处理UDP数据包。但是,它的代码在portThread线程内!我想那个handler.postDelayed(this,queryInterval);我用它来每隔100ms循环一次线程会导致下一个循环在UI线程而不是我的工作线程中运行。
所以我的问题是我在这里做错了怎么解决?或者,如何在每100毫秒正确地完成循环工作?我也不确定在何处放置Handler,因为我已经看到了它在Activity里面和worker-thread里面的例子。
答案 0 :(得分:1)
好吧,我认为我已经开始工作了,虽然我对此并不满意,所以不加以考虑。
基本上我最终使用TimerTask每隔100ms运行一次代码并通过Handler通知UI线程。我不确定这是不是最好的选择(我听说定时器不是那么好)但似乎有效:
dataStreamTimer = new Timer();
dataStreamTask = new TimerTask()
{
public void run()
{
if (portList == null)
{
portList = new ArrayList<PortListener>();
for (int i = minPort; i < maxPort; i++)
{
portList.add(new PortListener(i));
}
}
if (!stop)
{
ManagerUdpPacket p = portList.get(0).receive();
threadMsg("moi", p);
//handler.postDelayed(this, queryInterval);
//stop thread until next query
try {
synchronized(this){
this.wait(queryInterval);
}
} catch (InterruptedException e) {
Log.e("ERR", "InterruptedException in TimerTask.run");
}
}
else
{
//execution has been stopped, clear data:
//stop execution and close ports
for (int i = 0; i < portList.size(); i++)
{
portList.get(i).close();
}
}
}
答案 1 :(得分:0)
并不真正了解处理程序的目的。为什么你不只是在背景线程上准备数据而不是使用myActivity.runOnUIThread()
来运行updateContent()方法?也许p.getOperationTime()
被认为是网络操作,尝试将此值保存到后台线程中的某个变量,而不是通过UI线程发布它。