Android我在子线程中接受数据,想把接受完整的数据显示到popupwindows中
大家讲道理
大家讲道理 2017-04-17 17:56:49
0
2
423

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

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
阿神

Some people don’t understand the logic. Where does outStream come from? If you want to display the data in the thread in real time on the popupWindow, it is correct to use handle to throw the data, but why is the display of the popupWindow related to the stream? It is not just Is it enough to reset the value on popupWindow? (Similar to giving the data in the handle directly to popWindow’s textView.setText())

洪涛

The main thread updates the ui, obtains data, and updates the ui by the main thread through callbacks

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template