android 按钮事件 子线程,Android我在子线程中接受数据,想把接受完整的数据显示到popupwindows中...

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时,是不是接受线程停止了,有什么办法能显示数据?求助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值