为了账号安全,请及时绑定邮箱和手机立即绑定

Android udp通讯循环接收数据问题!

Android udp通讯循环接收数据问题!

慕雪9649571 2017-10-23 16:26:06
我最近在做一个关于udpserver不断接收客户端发来的数据在更新ui界面,可是在切换界面之后就会出现手机卡死现象,不知道是什么情况!
查看完整描述

1 回答

?
step_Android

TA贡献41条经验 获得超11个赞

贴log来看下啊。这样说很难找bug的。不过你可以尝试下,切换页面关了UDP先啊

查看完整回答
反对 回复 2017-10-23
  • 慕雪9649571
    慕雪9649571
    public class UDPServer implements Runnable { private byte[] msg = new byte[1024]; private boolean life = true; private DatagramSocket dSocket = null; public UDPServer() { try { dSocket = new DatagramSocket(17009); } catch (SocketException e) { e.printStackTrace(); } } /** * @return the life */ public boolean isServer() { return life; } /** * @param lifes the life to set */ public void setServer(boolean lifes) { this.life = lifes; LogUtil.e("dSocket.isClosed()show=="); if (life == false && dSocket != null) { if (!dSocket.isClosed()) { dSocket.close(); } dSocket.disconnect(); // dSocket = null; } } @Override public void run() { DatagramPacket dPacket = new DatagramPacket(msg, msg.length); while (life) { try { Thread.sleep(5);
  • 慕雪9649571
    慕雪9649571
    public void run() { DatagramPacket dPacket = new DatagramPacket(msg, msg.length); while (life) { try { Thread.sleep(5); if (dSocket != null) { dSocket.receive(dPacket); String datas = ByteUtil.byte2HexStr(dPacket.getData(), dPacket.getLength()); //将收到的消息发给主界面 if (dPacket.getAddress() != null) { Message message = new Message(); message.obj = dPacket.getAddress() + "--" + datas; message.what = 2; myHandler.sendMessage(message); } } else { life = false; } } catch (IOException e) { life = false; if (dSocket != null) { if (!dSocket.isClosed()) { dSocket.close(); } dSocket.disconnect(); }
  • 1 回答
  • 0 关注
  • 1362 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信