接收多个UDP数据包,同时更新UI [英] Receiving multiple UDP packets while updating UI
问题描述
我有一个应用程序,我从服务器接收信息,然后显示在屏幕,为用户的信息。由于有大量的信息,我想,因为我收到的信息进行更新的用户界面。
I have an application where I am receiving information from a server and then showing that information on the screen for the user. Since there is a lot of information, I would like to update the UI as I receive the information.
发送/接收是在一个单独的线程中完成的。
Sending/Receiving is done on a separate thread.
两个问题:
-
我怎样才能最好的接收多个UDP数据包?
我现在的code,用以接收一个数据包
How can I best receive multiple UDP packets? My current code for receiving one packet
try {
Log.i(TAG,"Listening...");
_dcOut.setSoTimeout(20000);
_dcOut.receive(packet);/* Wait to receive a datagram */
haveDatagram = true;
Log.d(TAG,"dc_out, received...");
}
catch (Exception e) { // can be just a time out
haveDatagram = false;
Log.d(TAG,"dc_out, failed to receive...");
}
是否可以同时接受多个UDP数据包更新的用户界面?
Is it possible to update UI while receiving multiple UDP packets?
编辑:
我使用的是绑定的服务,从服务器获取的信息(AIDL要具体)。下面是设置:
I am using a bound service to get the information from the server(AIDL to be specific). Here is the setup:
要么我:
1.获得一个单独的对象,并发送回,就是这样的服务的特定实例或
2.我可以发送回它们的列表为这项服务
Either I: 1. get an individual object and send it back and that's that for that particular instance of the service or 2. I can send back a List of them for that service
我的想法是,我应该发回的发言权,5-10对象的列表,并重复了一会儿?
My idea is that I should send back a list of say, 5-10 objects, and repeat that for a while?
- 我觉得好像没有为我被更新的用户界面,而与此服务设置接收数据包的方式 -
--I feel like there isn't a way for me to be updating the UI while receiving the packets with this service setup--
推荐答案
如果UDP数据包的接收是在一个单独的线程中完成的,应该没有问题,显示在您的GUI呢!
If the receiving of UDP packets are done on a seperate thread, there should be no problems showing it on your GUI!
您code表示只receving UDP数据。我需要更多的信息是具体的:)
Your code shows only receving UDP data. I need more info to be specific :)
只有一个UDPSocket处理一个特定的端口上的传入数据,它们都将(数据包)被顺序地存储在缓冲器中,专用于该特定处理
Only one UDPSocket handles incoming data on a specific port, they will all (packets) be stored sequentially in a buffer, dedicated to that specific process.
这篇关于接收多个UDP数据包,同时更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!