请问android的udp或者tcp是不是隔一段时间不操作,就自动关闭了?我用udp接收广播消息,隔一段时间不连接,就连不上了,怎么回事?
while (this.isAlive()) {
try {
DatagramSocket udp = new DatagramSocket(UdpServerPort);
try {
DatagramPacket pkt = new DatagramPacket(buf, 1024);
udp.receive(pkt);
String sTest = new String(pkt.getData(), 0, pkt.getLength());
if (sTest.compareToIgnoreCase("test") == 0) {
udp.send(pkt);
}
} finally {
udp.close();
}
} catch (Exception e) {
}
}
while (this.isAlive()) {
try {
DatagramSocket udp = new DatagramSocket(UdpServerPort);
try {
DatagramPacket pkt = new DatagramPacket(buf, 1024);
udp.receive(pkt);
String sTest = new String(pkt.getData(), 0, pkt.getLength());
if (sTest.compareToIgnoreCase("test") == 0) {
udp.send(pkt);
}
} finally {
udp.close();
}
} catch (Exception e) {
}
}
抱歉,我没有完全了解 Android Wifi的机制,确实是有自动停止网络服务的设定,见 WifiManager的lock方法。