- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直被一个问题困扰。我需要一些人检查我的部分代码并帮助我解决问题并批评我的代码(我编写代码,但我没有人可以说这是错误的或这种模式中的某些内容)
一般情况下。
我的服务从蓝牙 (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个超声波传感器的距离数据,每次持续几毫秒。
这是当我有调试日志时如何查看服务中的数据的屏幕。
下一个想法,但仍然一无所获:
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 将通过获取下一条消息进行刷新。
最佳答案
好的这篇文章可以帮助我解决问题: Sending a simple message from Service to Activity
也许这个链接可以帮助其他人。感谢您的帮助,现在明白为什么我应该使用广播接收器来执行此操作。
关于java - 如何使用 runOnUiThread 获取从服务到 Activity 的每次消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59428297/
我在数据库处理程序中构建了一个函数来使用 jsoup 从 sqlite + html 页面中获取数据。 如何设置 runOnUiThread 来处理数据库 habdler 函数中的线程数据? 或者我如
我收到了用户的错误报告,但我不知道我的代码出了什么问题。 logcat 是这样的: java.lang.NullPointerException at com.laurenswuyts.find.it
我想在我构建的应用程序小部件中发出 HTTP 请求。 这个想法是每隔几个小时,小部件就会通过发出 HTTP 请求来更新自己,并使用它的信息来更新它的 View 。 我已经尝试在 onUpdate 函数
事情是这样的:我正在使用 runOnUiThread() 方法从另一个类接收到来自另一部手机的消息时更改该类的 ImageView 。我通过使用 mainclass.runOnUiThread(new
我有 MainActivity(它扩展了 Activity)和 GameView(它扩展了 SurfaceView)。 MainActivity 有用于得分和剩余时间的 TextView。GameVi
我有一个 AsyncTask 正在运行,在这个线程中我启动了一个 runOnUiThread 来操作我的 View 。如果我关闭我的 AsyncTask,runOnUiThread 在我关闭我的 As
要从其他线程在主线程中运行某些内容,我们调用 runOnUiThread()。问题是,这个调用是同步的还是异步的? 如果我想在主线程中同步运行一些东西,我该怎么做? 最佳答案 您不能“在主线程中异步运
我的 android 应用程序存在内存泄漏。这是一个简单的音乐播放器。在屏幕底部,我有一个 TextView,用于显示耗时。它在下面的线程中更新。 每次我改变方向时,堆大小都会增加。通过查看 DDMS
如果考虑到效率,做following可以吗? new Runnable() { @Override public void run() { tr
我正在创建一个带有 ListView 的下载管理器,它将通过进度条显示当前下载的内容。我可以知道如何更新进度条吗? 例如。我有3节课。 "main.class"= 是一个 ListView 。 "mA
我有这个问题。我正在尝试从另一个线程更新我的 TextView 但它不允许我这样做。 我尝试了很多不同的解决方案,但似乎都没有帮助。在我的 while 循环代码中,一直打印“Started new l
我的应用程序应该连接到服务器并从中接收数据。但由于此错误而无法正常工作: android cannot resolve method runOnUiThread(anonymous java.lang
我正在开发一个使用第三方 tts 的应用程序,名为 flite。我正在做类似 tts 说一句话的事情。我想突出显示每个单词。为此,我设法从 tts 获得了单词级别的回调。工作流程是这样的——按下“说出
我一直在研究 runOnUiThread 方法。如果我只是在我的 Activity 中创建一个方法: public void Test() { runOnUiThread(new Runnab
我找到了 this这里有一个关于如何使用 runOnUiThread 方法的示例,但我不明白如何使用它。 我在主要 Activity 中设置了列表适配器 // Set a gobal refe
我有一个 fragment 叫做 MyRequestFragment ,其中包含一个 RecyclerView并将数据绑定(bind)到 RecyclerView使用 onBindViewHolder
我试图从我的后台线程在 UI 线程上抛出和警告对话框,但我遇到了 runOnUiThread 未定义的问题。我已经尝试过 FindLocation.this.runOnUiThread 和 runOn
我有这段代码: public void updateOptionLists() { Log.d("UI", "Called update"); if (updating){
这个问题在这里已经有了答案: Accessing UI thread handler from a service (7 个回答) 关闭8年前。 我想创建一项服务,如果有任何未读的 SMS,则每 20
我试图在我的一项 Activity 的 onCreate() 方法期间显示进度对话框,在线程中完成填充屏幕的工作,然后关闭进度对话框。 这是我的 onCreateMethod() dialog = n
我是一名优秀的程序员,十分优秀!