Blame view

C5/app/src/main/java/com/sunvote/xpadcomm/DownloadFileSendThread.java 3.04 KB
fac86401   孙向锦   初始化C5 Vote
1
2
3
4
5
6
7
8
  package com.sunvote.xpadcomm;
  
  import android.os.Handler;
  import android.os.HandlerThread;
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
18faef8d   孙向锦   下载修改
9
  import java.net.InetAddress;
fac86401   孙向锦   初始化C5 Vote
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  import java.net.SocketException;
  
  import com.sunvote.util.LogUtil;
  
  /**
   * Created by Elvis on 2017/12/25 15:38
   * Email:Eluis@psunsky.com
   * 版权所有:长沙中天电子设计开发有限公司
   * Description: 人大通用版XPadAppRD
   * 下载文件发送数据请求线程
   */
  public class DownloadFileSendThread extends HandlerThread {
  
      private static String TAG = "DownloadFileSendThread" ;
  
      private DatagramSocket udpSocket ;
      private DatagramPacket packet;
      private Handler handler;
  
      public DownloadFileSendThread() {
          super(TAG);
          start();
          handler = new Handler(getLooper());
      }
  
18faef8d   孙向锦   下载修改
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
      public boolean sendData(final byte flag,final byte type,final int index,final String ip,final int port){
          handler.post(new Runnable() {
              @Override
              public void run() {
                  byte[] buffer = new byte[10];
                  buffer[0] = (byte) 0xFE;
                  buffer[1] = (byte) 0xAA;
                  buffer[2] = flag;
                  buffer[3] = type;//
                  buffer[4] = (byte) ((index >> 8) & 0xff);
                  buffer[5] = (byte) (index & 0xff);
                  InetAddress servInetAddress;
                  try {
                      servInetAddress = InetAddress.getByName(ip);
                  } catch (Exception e) {
                      LogUtil.e(TAG, e);
                      return ;
                  }
                  LogUtil.i(TAG,"sendNextData",buffer);
                  packet = new DatagramPacket(buffer, buffer.length, servInetAddress, port);
                  handler.removeCallbacks(task);
                  handler.post(task);
              }
          });
fac86401   孙向锦   初始化C5 Vote
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
          return true;
      }
  
      private Runnable task = new Runnable() {
          @Override
          public void run() {
              if (handler != null) {
                  handler.removeCallbacks(this);
                  if (udpSocket == null) {
                      try {
                          udpSocket = new DatagramSocket();
                      } catch (SocketException e) {
                          LogUtil.e(TAG, e);
                      }
                  }
                  if (udpSocket != null && packet != null) {
                      try {
                          LogUtil.i(TAG,"send to server",packet.getData());
                          udpSocket.send(packet);
                      } catch (IOException e) {
                          LogUtil.e(TAG, e);
                          udpSocket.close();
                          udpSocket = null;
                      }
                  }
              }
              if(handler != null){
                  handler.postDelayed(this, 1000);
              }
          }
      };
  
      public void close(){
          handler.removeCallbacks(task);
          if(udpSocket != null){
              udpSocket.close();
              udpSocket = null;
          }
          handler = null;
          packet = null;
          quit();
      }
  }