gpt4 book ai didi

java - 如何使用 runOnUiThread 获取从服务到 Activity 的每次消息

转载 作者:行者123 更新时间:2023-12-01 19:15:06 25 4
gpt4 key购买 nike

我一直被一个问题困扰。我需要一些人检查我的部分代码并帮助我解决问题并批评我的代码(我编写代码,但我没有人可以说这是错误的或这种模式中的某些内容)

一般情况下。

我的服务从蓝牙 (HC-05) 获取消息,我可以在服务中看到 Log.d 中的值。

我的服务的一部分代码,用于获取消息。

private class ConnectedThread extends Thread{
private final BluetoothSocket bluetoothSocket;
private final InputStream inputStream;
private final OutputStream outputStream;

public ConnectedThread(BluetoothSocket socket){
Log.d(TAG,"ConnectedThread: Starting");

bluetoothSocket=socket;
InputStream tmpInput = null;
OutputStream tmpOutput = null;
try{
tmpInput = bluetoothSocket.getInputStream();
tmpOutput = bluetoothSocket.getOutputStream();

}catch (IOException e){
e.printStackTrace();
active=false;
}
inputStream=tmpInput;
outputStream=tmpOutput;

}

@Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while(active){
try {
bytes = inputStream.read(buffer);
final String comingMsg = new String(buffer,0,bytes);
Log.d(TAG,"InputStream: " + comingMsg);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Message message = new Message();
message.obj = comingMsg;
message.what = 1; // I need it to prevent NullObjReference
Log.d(TAG,"Handler run(): " + message.obj);
mHandler.sendMessage(message);
}
});
}catch (IOException e){
Log.e(TAG,"Write: Error reading input." + e.getMessage());
active=false;
break;
}
}
}
...some code is hidden because it is diploma thesis
}

问题是每次从该服务到另一个发生所有情况的 Activity 时都会收到消息。我尝试了很多东西(使用Threads,Looper,runOnUiThread,handleMessage和回调),检查了stackoverflow中的很多帖子,我尝试与我的项目结合起来,但一直以来我都有nullobjectreference(为此我尝试使用msg.what检查),当尝试移动到我的家庭 Activity (它是主要的)并更新我的 textView 或典型的崩溃应用程序时黑屏。

现在我只想从服务获取消息到 TextView 。当一切开始正常工作时,我想解析字符串(例如前 3 个字符)并将消息发送到六个 TextView 之一。

runThread()方法启动之前onCreate的部分代码:

Log.d(TAG,"Check intent - result");
if(getIntent().getIntExtra("result",0)==RESULT_OK){
mDevice = getIntent().getExtras().getParcelable("bonded device");
startConnection(mDevice,MY_UUID);
Log.d(TAG,"Check is active service ");
checkIfActive();;
}

Log.d(TAG,"Check intent - connect_to_paired");
if(getIntent().getIntExtra("connect_to_paired",0)==RESULT_OK){
mDevice = getIntent().getExtras().getParcelable("bonded_paired_device");
startConnection(mDevice,MY_UUID);
Log.d(TAG,"Check is active service ");
checkIfActive();

}

public void checkIfActive(){
Log.d(TAG,"CheckIfActive: Started");
while(myBluetoothService.active!=true) {
Log.d(TAG,"CheckIfActive() active is "+ myBluetoothService.active);
if (myBluetoothService.active) {
Log.d(TAG, "CheckIfActive: Running method runOnUiThread - myBluetoothService.active is "+myBluetoothService.active);
runThread();
}
}
}

方法 runThread() 在每次连接蓝牙设备后都应该起作用:

public void runThread(){
//I used there Thread but when connection was fail,
// method created multiply threads when I tried to connect next time
runOnUiThread(new Runnable() {
@Override
public void run() {
handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
while (true) {
switch (msg.what) {
//when is one, service has messages to send
case 1:
String message = myBluetoothService.mHandler.obtainMessage().toString();
rearLeft.setText(message);
break;
default:
super.handleMessage(msg);
}
}
}
};
}
});

}

更新:

这是个好主意吗?也许我可以将 JSON 对象放入服务中以发送消息,并在 HomeActivity 中,我可以尝试从 JSON 获取值。快吗?我发送了大量数据,因为蓝牙分四次接收来自4个超声波传感器的距离数据,每次持续几毫秒。

这是当我有调试日志时如何查看服务中的数据的屏幕。

enter image description here

下一个想法,但仍然一无所获:

HomeActivity(我的主要)

public void runThread(){
runOnUiThread(new Runnable() {
@Override
public void run() {
//Looper.prepare();
new Handler() {
@Override
public void handleMessage(Message msg) {
rearLeft.setText(msg.obj.toString());

}
};
//Looper.loop();
//Log.d(TAG, myBluetoothService.mHandler.getLooper().toString());
//rearLeft.setText(myBluetoothService.mHandler.getLooper().toString());

}
});

}

应将数据从蓝牙发送到 UI 线程的服务是相同的(检查第一个代码)。

HomeActivity 的屏幕,您可以在其中看到 6 个 TextView 。现在我想将所有文本放入一个 View 中,该 View 将通过获取下一条消息进行刷新。

enter image description here

最佳答案

好的这篇文章可以帮助我解决问题: Sending a simple message from Service to Activity

也许这个链接可以帮助其他人。感谢您的帮助,现在明白为什么我应该使用广播接收器来执行此操作。

关于java - 如何使用 runOnUiThread 获取从服务到 Activity 的每次消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59428297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com