Maison > Questions et réponses > le corps du texte
1.但是一直得不到数据……
这个是接受数据线程
private class ConnectedThread extends Thread {
private final BluetoothSocket socket;
private final InputStream inputStream;
private final OutputStream outputStream;
public ConnectedThread(BluetoothSocket socket) {
this.socket = socket;
InputStream input = null;
OutputStream output = null;
try {
input = socket.getInputStream();
output = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
this.inputStream = input;
this.outputStream = output;
}
public void run() {
byte[] buff = new byte[20];
int bytes;
while (true) {
try {
bytes = inputStream.read(buff);
String str = new String(buff, "ISO-8859-1");
str = str.substring(0, bytes);
Log.e("recv", str);
System.out.println("**log:"+str);
Message message=handler_accept.obtainMessage();
message.obj=str;
handler_accept.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
public void write(byte[] bytes) {
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
public void cancel() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
---这个是handler
Handler handler_accept=new Handler(){
@Override
public void handleMessage(Message msg) {
String s= (String) msg.obj;
timeStr=timeStr+s;
//数据
}
}
}
};
popupwindows,这个显示调用方法是在按钮的点击事件中
private void showPopupWindow(View view) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(Zhuye_Activity.this).inflate(R.layout.layout_timeupdate, null);
// 设置按钮的点击事件
final PopupWindow popupWindow = new PopupWindow(contentView,
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
WindowManager manager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
popupWindow.setTouchable(true);
int xpos=manager.getDefaultDisplay().getWidth()/2-popupWindow.getWidth()/2;
//xoff,yoff基于anchor的左下角进行偏移。
TextView textView1 = (TextView) contentView.findViewById(R.id.textView_xiugai);
TextView textView2 = (TextView) contentView.findViewById(R.id.textView_quxiao);
final TextView textView_shebeitime= (TextView) contentView.findViewById(R.id.textView_shebeitime);
textView_shebeitime.setText(textView1_neiyong.getText().toString());
TextView textView_nowtime= (TextView) contentView.findViewById(R.id.textView_nowtime);
SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date=sDateFormat.format(new java.util.Date());
textView_nowtime.setText(date);
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { screenshot();
String s=Utils.Timeform();
try {
outStream.write(s.getBytes());
outStream.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
});
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});*斜体文字*
那么问题来了。我一点击显示popupwindows,数据一直是null,有什么办法能让pop得到数据呢,
本来我加个了sleep,结果不行、对这些还是不了解,sleep时,是不是接受线程停止了,有什么办法能显示数据?求助
阿神2017-04-17 17:58:49
有的看不懂逻辑,outStream是怎么来的,假如你想把线程中的数据实时显示到popupWindow上,用handle抛出数据是正确的,但是为什么popupWindow的显示会跟流有关,不是只需要在popupWindow上重新设置值就可以了吗?(类似把handle里面的数据直接给popupWindow的textView.setText())