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

时间:2018-05-30 12:40:54

标签: java android textview android-broadcast

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

这是我调用run方法的地方。

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

这是我在另一个类中的run方法。

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

                            }
                        });

        }


    });

1 个答案:

答案 0 :(得分:0)

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

 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;
        }
    }

}

});