刚刚学会使用定时器 写下来以防忘记
//实例化定时器
private Timer timer=new Timer();
private InputStream is=null;
//自己定一个byte数组 长度要大于数据长度
byte[] buffer=new byte[64];
int bytes;
/**
* 定时器操作
*/
private void dopost(){
TimerTask task=new TimerTask() {
@Override
public void run() {
try {
//判断是否为空
if(is!=null){
//socket
is=getsocket().getInputStream();
//buffer是我自定的一个byte数组 向其中写入数据
bytes=is.read(buffer);
if (bytes!=0){
//将自己定义的数组长度改为数据的长度
byte[] by=new byte[bytes];
//for循环 根据数据长度遍历循环
for (int i=0; i<bytes;i++){
//赋值
by[i]=buffer[i];
}
//调取HexUtil工具类解析数据 工具类是拷过来的不是自带的
Log.e(this.getClass().getName(), HexUtil.encodeHexStr(by));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
timer.schedule(task,0,20);
}