TextView.setText 由于线程问题无法正常工作?

TextView.setText not working due to threading?

我正在 运行 连接一个线程,该线程每秒数次从设备接收一串数据。我正在尝试更新 TextView 以显示该数据,但是一旦我在 connector.run() 中启动线程,我就无法设置 TextView 的文本。即使我 运行 运行 方法上方的 setText() 它也不起作用,除非我注释掉 运行 方法调用。

这里是我调用 运行 方法的地方。

readWeight.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //inputWindow.setText("helloooooooo worldddddd");
                    connector.run();
                    setInputWindow();
                   //readWeight.setVisibility(View.INVISIBLE);
                }
            });

这是我在另一个 class 中的 运行 方法。

public void run() {
            // Keep listening to the InputStream while connected
            while (true) {

                try {
                    output = "";
                    //read the data from socket stream
                    if(mmInStream != null) {
                        mmInStream.read(buffer);
                      for(byte b : buffer)
                      {
                          char c = (char) b;
                          if(c >=' ' && c <'z') {
                           // System.out.print(c);
                            output += c;
                          }

                      }
                       System.out.println();
                        Intent intent = new Intent();
                        intent.setAction("com.curie.WEIGHT_RECEIVED");
                        intent.putExtra("Output",output);
                        LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);

                        // LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcast(intent);

                    }
                    // Send the obtained bytes to the UI Activity
                } catch (IOException e) {
                    //an exception here marks connection loss
                    //send message to UI Activity
                    break;
                }
            }
        }

当我检查 BroadcastReceiver 收到的字符串时,我正在正确接收数据,所以这不是问题所在。

另外,我不需要每次都更新屏幕,我可以每隔一秒左右更新一次,这样会更好吗?谢谢你。

编辑:添加的处理程序未编译。请帮忙。 new Handler().post(new Runnable() {

        @Override
        public void run() {
            // Always cancel discovery because it will slow down a connection
            //Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");
            while (true) {
                //counter++;

                try {
                    output = "";
                    //read the data from socket stream
                    //mmInStream != null && counter%10000000 == 1
                    if(mmInStream != null) {
                        mmInStream.read(buffer);
                        for(byte b : buffer)
                        {
                            char c = (char) b;
                            if(c >=' ' && c <'z') {
                                // System.out.print(c);
                                output += c;
                            }

                        }
                        System.out.println();
                        Intent intent = new Intent();
                        intent.setAction("com.curie.WEIGHT_RECEIVED");
                        intent.putExtra("Output",output);
                        LocalBroadcastManager.getInstance(inputContext).sendBroadcastSync(intent);

                        // LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcast(intent);

                    }
                    // Send the obtained bytes to the UI Activity
                } catch (IOException e) {
                    //an exception here marks connection loss
                    //send message to UI Activity
                    break;
                }
            }
 inputContext.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // here do the UI chnages
                                //  you can set text in textview Here

                            }
                        });

        }


    });

对于后台工作使用处理程序(使用此代码)

 new Handler().post(new Runnable() {
            @Override
            public void run() {
                // here do the background task

            }
        });

如果你想更新 UI 使用 runOnUiThread()

              if u want  to update ui Just do this
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // here do the UI chnages
                    }
                });

希望对您有所帮助。

像这样

  @Override
public void run() {
    // Always cancel discovery because it will slow down a connection
    //Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");
    while (true) {
        //counter++;

        try {
            output = "";
            //read the data from socket stream
            //mmInStream != null && counter%10000000 == 1
            if(mmInStream != null) {
                mmInStream.read(buffer);
                for(byte b : buffer)
                {
                    char c = (char) b;
                    if(c >=' ' && c <'z') {
                        // System.out.print(c);
                        output += c;
                    }

                }
               // context == activity context
               context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // here do the UI chnages
                      //  you can set text in textview Here
                    }
                });


            }
            // Send the obtained bytes to the UI Activity
        } catch (IOException e) {
            //an exception here marks connection loss
            //send message to UI Activity
            break;
        }
    }

}

});