[英]How do I send data from thread back to main UI?
void beginListenForData() {
//final Handler handler = new Handler();
final Handler handler = new Handler(Looper.getMainLooper());
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[2048];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
int bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable() {
public void run() {
//myLabel.setText(data);
dataArray = new String []{data};
//Log.d("dataArray", data);
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
public class Bluetooth_dataDisplay extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
String MAC = getIntent().getStringExtra("MAC");
mAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);
// Initiate a connection request in a separate thread
ConnectingThread t = new ConnectingThread(bluetoothDevice);
t.start();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerAdapter(dataArray);
recyclerView.setAdapter(adapter);
}
Begindata() is call in the connected thread. 在连接的线程中调用Begindata()。 I trying to get the dataarray back to the Bluetooth_dataDisplay extends AppCompatActivity and call it in the oncreate. 我试图将数据数组返回到Bluetooth_dataDisplay扩展AppCompatActivity并在oncreate中调用它。 How do I senddataarray back to main activity? 如何将dataarray发送回主要活动? Please help any expert. 请帮助任何专家。 I had look at post that talked about thread data send back to main UI. 我看过一篇有关将线程数据发送回主UI的文章。 But I am very new to it, so it quite confusing. 但是我对此很陌生,因此非常混乱。 :( Help please. :( 请帮助。
Replace handler call under your beginListenForData()
function 在beginListenForData()
函数下替换处理程序调用
handler.post(new Runnable() {
public void run() {
//myLabel.setText(data);
dataArray = new String[]{data};
//Log.d("dataArray", data);
}
});
with runOnUiThread()
与runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
//myLabel.setText(data);
dataArray = new String[]{data};
//Log.d("dataArray", data);
}
});
Everything under runOnUiThread()
runs on UI/Main thread. runOnUiThread()
下的所有内容都在UI / Main线程上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.