android开发:Socket对象使用心跳机制实现服务器与客户端通信

/**
* 心跳线程,每隔一段时间与服务器通信,发送的数据是:1,也可以制定你们想要的数据
*/
private Runnable HeartBeatRunnable = new Runnable() {
@Override
public void run() {
if(System.currentTimeMillis() – lastTime >= TAKT_TIME){
boolean status = sendMsg(“1″);
if(!status){
mHandler.removeCallbacks(HeartBeatRunnable);
releaseLastSocket(mSocket);
mReadThread.release();//释放线程
initSocket();//重建线程
}
mHandler.postDelayed(HeartBeatRunnable, TAKT_TIME);//继续发送心跳包
Log.d(TAG,”HeartBeatRunnable”);
}
}
};

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |