Android 从 Inputstream 接收数据时 TextView 文本更新
Android TextView text updation on receiving data from Inputstream
我正在编写一个客户端-服务器应用程序来测试两个 Android 蓝牙设备之间的通信。在我的客户端上,我正在从输入流对象中读取数据。
当我通过在 logcat 上打印检查时,数据正在被成功读取。但是,当我尝试将数据设置为 TextView 时,它没有显示。
在下面的代码中,packetsReceivedTV 是 TextView 对象。当我在 logcat 上打印 'result' 时,我得到了正确的输出,但文本未在 TextView 中设置。是不是因为我在while(监听)循环中设置文本?
while(listening){
bytesRead =instream.read(buffer);
if(bytesRead!=-1){
String dataRead= new String(buffer,"UTF-8");
System.err.println("*************result : "+dataRead);
packetsReceivedTV.setText("Received : "+dataRead);
packetsReceivedTV.invalidate();
}
}
甚至调用 invalidate() 也不起作用。
注意: 有时,当我终止一台设备上的服务器进程时,客户端设备上的 TextView 会正确更新。但这并不总是发生。请帮忙!
不清楚你的代码是在哪个线程上调用的,但你需要确保这两个操作发生在不同个线程上:
- 流轮询需要在后台线程上完成。如果您不这样做,那么您将看不到文本,因为
read()
上的线程阻塞使主线程无法更新 UI 元素。
setText()
方法调用必须发生在主 (UI) 线程上。如果您不这样做,文本也不会显示——在某些设备上您甚至会看到崩溃。
我假设此代码存在于 activity 中(因为您正在尝试更新 UI 元素)。 虽然这不是最佳实践,但下面是一个演示该概念的简单示例:
Thread pollingThread = new Thread() {
@Override
public void run() {
…
//This code needs to be running on a background thread
while(listening){
bytesRead = instream.read(buffer);
if(bytesRead != -1){
String dataRead= new String(buffer,"UTF-8");
System.err.println("*************result : "+dataRead);
runOnUiThread(new Runnable() {
@Override
public void run() {
//This code needs to be posted back to the main thread.
packetsReceivedTV.setText("Received : "+dataRead);
}
});
}
}
}
};
//Start the listener thread
pollingThread.start();
这真的只是为了说明轮询代码必须在后台和视图代码必须在主线程上。
Even a call to invalidate() is not working.
TextView
当它的内容改变时在内部调用它,所以你调用它是多余的。
我正在编写一个客户端-服务器应用程序来测试两个 Android 蓝牙设备之间的通信。在我的客户端上,我正在从输入流对象中读取数据。 当我通过在 logcat 上打印检查时,数据正在被成功读取。但是,当我尝试将数据设置为 TextView 时,它没有显示。
在下面的代码中,packetsReceivedTV 是 TextView 对象。当我在 logcat 上打印 'result' 时,我得到了正确的输出,但文本未在 TextView 中设置。是不是因为我在while(监听)循环中设置文本?
while(listening){
bytesRead =instream.read(buffer);
if(bytesRead!=-1){
String dataRead= new String(buffer,"UTF-8");
System.err.println("*************result : "+dataRead);
packetsReceivedTV.setText("Received : "+dataRead);
packetsReceivedTV.invalidate();
}
}
甚至调用 invalidate() 也不起作用。
注意: 有时,当我终止一台设备上的服务器进程时,客户端设备上的 TextView 会正确更新。但这并不总是发生。请帮忙!
不清楚你的代码是在哪个线程上调用的,但你需要确保这两个操作发生在不同个线程上:
- 流轮询需要在后台线程上完成。如果您不这样做,那么您将看不到文本,因为
read()
上的线程阻塞使主线程无法更新 UI 元素。 setText()
方法调用必须发生在主 (UI) 线程上。如果您不这样做,文本也不会显示——在某些设备上您甚至会看到崩溃。
我假设此代码存在于 activity 中(因为您正在尝试更新 UI 元素)。 虽然这不是最佳实践,但下面是一个演示该概念的简单示例:
Thread pollingThread = new Thread() {
@Override
public void run() {
…
//This code needs to be running on a background thread
while(listening){
bytesRead = instream.read(buffer);
if(bytesRead != -1){
String dataRead= new String(buffer,"UTF-8");
System.err.println("*************result : "+dataRead);
runOnUiThread(new Runnable() {
@Override
public void run() {
//This code needs to be posted back to the main thread.
packetsReceivedTV.setText("Received : "+dataRead);
}
});
}
}
}
};
//Start the listener thread
pollingThread.start();
这真的只是为了说明轮询代码必须在后台和视图代码必须在主线程上。
Even a call to invalidate() is not working.
TextView
当它的内容改变时在内部调用它,所以你调用它是多余的。