public class ClientAgainConnect implements Runnable {
private boolean isReConn = true;
private static final int DELAY_TIME = 5000;
private Socket mClientSocket = null;
private Handler mHandler;
private int mType = -1;
public void run() {
while (isReConn) {
holdTime(DELAY_TIME);
}
}
public void closeReConn(){
isReConn = false;
}
public ClientAgainConnect(Socket clientSocket,Handler handler,int type) {
this.mType = type;
this.mClientSocket = clientSocket;
this.mHandler = handler;
initiate();
}
private void holdTime(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
sendReConnData();
}
private void sendReConnData() {
try {
if (mClientSocket != null && mClientSocket.isConnected()) {
mClientSocket.sendUrgentData(0xFF);
}
} catch (IOException e) {
mHandler.sendEmptyMessage(mType);
initiate();
}
}
// 启动重连线程
private void initiate() {
new Thread(ClientAgainConnect.this).start();
}