如何使用 runOnUiThread 每次从服务获取消息到 activity
How to get every time messages from service to activity using runOnUiThread
我一直被一个问题困扰。我需要一些人来检查我的部分代码并帮助我解决问题并批评我的代码(我编写代码但我没有人可以说这是错误的或这种模式的东西)
一般。
我的服务从蓝牙 (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 和回调),在 Whosebug 中检查了很多帖子,我尝试结合我的项目,但我一直都有 nullobjectreference(为此我尝试使用 msg.what 检查),尝试移动到我的家时黑屏 activity(这是主要的)并更新我的 textView 或典型的崩溃应用程序。
现在我只想从服务获取消息到文本视图。当一切开始正常工作时,我想解析字符串(例如前 3 个字符)并将消息发送到六个文本视图之一。
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 个超声波传感器接收了 4 次距离数据,每次持续到几毫秒。
这是当我有调试日志时如何查看我的服务数据的屏幕。
下一个想法,但仍然没有:
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 个文本视图。现在我想将所有文本放在一个视图中,该视图将通过获取下一条消息进行刷新。
好的 post 有点帮助我解决问题:
也许这个 link 可以帮助其他人。
感谢您的帮助,现在明白为什么我应该使用广播接收器来做这个了。
我一直被一个问题困扰。我需要一些人来检查我的部分代码并帮助我解决问题并批评我的代码(我编写代码但我没有人可以说这是错误的或这种模式的东西)
一般。
我的服务从蓝牙 (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 和回调),在 Whosebug 中检查了很多帖子,我尝试结合我的项目,但我一直都有 nullobjectreference(为此我尝试使用 msg.what 检查),尝试移动到我的家时黑屏 activity(这是主要的)并更新我的 textView 或典型的崩溃应用程序。
现在我只想从服务获取消息到文本视图。当一切开始正常工作时,我想解析字符串(例如前 3 个字符)并将消息发送到六个文本视图之一。
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 个超声波传感器接收了 4 次距离数据,每次持续到几毫秒。
这是当我有调试日志时如何查看我的服务数据的屏幕。
下一个想法,但仍然没有:
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 个文本视图。现在我想将所有文本放在一个视图中,该视图将通过获取下一条消息进行刷新。
好的 post 有点帮助我解决问题:
也许这个 link 可以帮助其他人。 感谢您的帮助,现在明白为什么我应该使用广播接收器来做这个了。