DownloadFileSendThread.java
2.16 KB
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.sunvote.xpadcomm;
import android.os.Handler;
import android.os.HandlerThread;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
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());
}
public boolean sendData(DatagramPacket packet){
this.packet = packet;
handler.removeCallbacks(task);
handler.post(task);
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();
}
}