Android socket 开发,线程CPU占用过高
代码如下,怀疑是while(true)的问题,但不知道如何去修改,望大大帮忙privateThread_thread=newThread(){publicvoidrun(...
代码如下,怀疑是while(true)的问题,但不知道如何去修改,望大大帮忙
private Thread _thread = new Thread(){
publicvoid run(){
while(ifdodo){
try{
InputStream in = _socket.getInputStream();
byte[] buffer = newbyte[in.available()];
in.read(buffer);
String mm="";
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(buffer[i] & 0xFF);
if (hex.length() == 1) {
mm += '0' + hex+" ";
}else{
mm+=hex+" ";
}
}
buffer[1] = 1;
int aaa = buffer[1];
System.out.println(Integer.toString(aaa));
String msg=mm;//转成16进制
if(msg.length() != 0){
// 在控制台中输出接收到的信息
System.out.println("in: " + msg);
// 该部分是传参并更新控件
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg", msg);
message.setData(bundle);
// 发送消息到handler
_handler.sendMessage(message);
}
}
catch (Exception e){
}
}
}
}; 展开
private Thread _thread = new Thread(){
publicvoid run(){
while(ifdodo){
try{
InputStream in = _socket.getInputStream();
byte[] buffer = newbyte[in.available()];
in.read(buffer);
String mm="";
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(buffer[i] & 0xFF);
if (hex.length() == 1) {
mm += '0' + hex+" ";
}else{
mm+=hex+" ";
}
}
buffer[1] = 1;
int aaa = buffer[1];
System.out.println(Integer.toString(aaa));
String msg=mm;//转成16进制
if(msg.length() != 0){
// 在控制台中输出接收到的信息
System.out.println("in: " + msg);
// 该部分是传参并更新控件
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putString("msg", msg);
message.setData(bundle);
// 发送消息到handler
_handler.sendMessage(message);
}
}
catch (Exception e){
}
}
}
}; 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询