- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个名为“bluetoothIn”的处理程序,我想使用将提供循环程序的 HandlerThread 类将它传递给一个单独的循环程序。但是,我需要将结果从“handleMessage(Message msg)”发布回 UI 线程,因为我无法从主线程以外的线程修改 UI 元素。
这是我的代码:
package com.uniproj.senseplate;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
@SuppressWarnings("unused")
public class MainActivity extends Activity {
Button btnscan;
TextView txtArduino, txtString, txtStringLength, calorie;
Handler bluetoothIn;
final int handlerState = 0; //used to identify handler message
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket; //= null;
private StringBuilder recDataString = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service - this should work for most devices
private static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// String for MAC address
private static String address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Link the buttons and textViews to respective views
btnscan = (Button) findViewById(R.id.scanBtn);
txtString = (TextView) findViewById(R.id.txtString);
txtStringLength = (TextView) findViewById(R.id.testView1);
bluetoothIn = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == handlerState) {
String readMessage = (String) msg.obj;
recDataString.append(readMessage);
int endOfLineIndex = recDataString.indexOf("~");
if (endOfLineIndex > 0) {
String dataInPrint = recDataString.substring(0, endOfLineIndex);
txtString.setText("Data Received = " + dataInPrint);
int dataLength = dataInPrint.length();
txtStringLength.setText("String Length = " + String.valueOf(dataLength));
if (recDataString.charAt(0) == '#')
{
//get sensor value from string between indices 1-20
String weight = recDataString.substring(1, 20);
//update the textviews with sensor values
calorie.setText(weight + "kg");
}
recDataString.delete(0, recDataString.length());
// strIncom =" ";
dataInPrint = " ";
}
}
}
};
btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
checkBTState();
// Set up onClick listeners for button to scan for data
btnscan.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mConnectedThread.write("0");
}
});
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
return device.createRfcommSocketToServiceRecord(BTMODULEUUID);
//creates secure outgoing connecetion with BT device using UUID
}
@Override
public void onResume() {
super.onResume();
//Get MAC address from DeviceListActivity via intent
Intent intent = getIntent();
//Get the MAC address from the DeviceListActivty via EXTRA
address = intent.getStringExtra(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
//create device and set the MAC address
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
Toast.makeText(getBaseContext(), "Socket creation failed", Toast.LENGTH_LONG).show();
}
// Establish the Bluetooth socket connection.
try
{
btSocket.connect();
} catch (IOException e) {
try
{
btSocket.close();
} catch (IOException e2)
{
//insert code to deal with this
}
}
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
//I send a character when resuming.beginning transmission to check device is connected
//If it is not an exception will be thrown in the write method and finish() will be called
mConnectedThread.write("x");
}
@Override
public void onPause()
{
super.onPause();
try
{
//Don't leave Bluetooth sockets open when leaving activity
btSocket.close();
} catch (IOException e2) {
//insert code to deal with this
}
}
//Checks that the Android device Bluetooth is available and prompts to be turned on if off
private void checkBTState() {
if(btAdapter==null) {
Toast.makeText(getBaseContext(), "Device does not support bluetooth", Toast.LENGTH_LONG).show();
} else {
if (btAdapter.isEnabled()) {
} else {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
//create new class for connect thread
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
btSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = mmInStream.read(buffer);
bluetoothIn.obtainMessage(handlerState, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
//write method
public void write(String input) {
byte[] msgBuffer = input.getBytes();//converts entered String into bytes
try {
mmOutStream.write(msgBuffer);//write bytes over BT connection via outstream
} catch (IOException e) {
//if you cannot write, close the application
Toast.makeText(getBaseContext(), "Connection Failed", Toast.LENGTH_LONG).show();
finish();
}
}
public class BluetoothInHandler extends Handler{
private Looper sLooper = null;
private Handler mWorkerThreadHandler;
final int handlerState = 0; //used to identify handler message
protected class WorkerArgs {
Handler handler;
String input;
String output;
}
public BluetoothInHandler() {
super();
synchronized (BluetoothInHandler.class) {
if (sLooper == null) {
HandlerThread thread = new HandlerThread("AsyncWorker");
thread.start();
sLooper = thread.getLooper();
}
}
mWorkerThreadHandler = new WorkerHandler(sLooper);
}
@Override
public void handleMessage(Message msg) {
if (msg.what == handlerState) {
WorkerArgs args = (WorkerArgs) msg.obj;
String readMessage = args.output;
//your job;
} else {
super.handleMessage(msg);
}
}
public void write(String input) {
WorkerArgs args = new WorkerArgs();
args.handler = this;
args.input = input;
Message message = mWorkerThreadHandler.obtainMessage(handlerState);
message.obj = args;
mWorkerThreadHandler.sendMessage(message);
}
protected class WorkerHandler extends Handler {
public WorkerHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
if (msg.what == handlerState) {
WorkerArgs args = (WorkerArgs) msg.obj;
//the code here run in a thread, not in the ui thread
//do your job like:
byte[] bytes = mmInStream.read(buffer);
args.output = new String(bytes);
Message message = args.handler.obtainMessage(handlerState);
message.obj = args;
message.sendToTarget();
}
}
}
}
}//ConnectedThread End
}
最佳答案
runOnUi 可能是一个不错的选择
runOnUiThread(new Runnable() {
@Override
public void run() {
// Do whatever you need to do on the UI here
}
});
关于java - 如何使用 HandlerThread 类将单独的循环器传递给线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30598827/
我一直在查看 HandlerThread 类,因为最初我使用的是一个简单的线程和处理程序,但在 android 中出现了带有 NetworkOnMainThreadException 的裁剪器。 我似
我正在使用 HandlerThread 来处理 Android 中的线程, public class HandlerTest extends HandlerThread { private s
我有一个扩展 HandlerThread 类的类,用于在需要时在 GridView 中从设备上的照片创建位图,但有时它不会创建照片的位图,而是什么都没有填充 GridView 项目(ImageView
HandlerThread thread = new HandlerThread("DownloadService"); thread.start(); 与线程关联的循环程序是否正在等待消息到达消息队
我正在尝试学习如何使用 HandlerThread 在后台运行。我在 fragment 中有一个按钮,单击该按钮后,我想从数据库中获取条目数。但是当我实际运行它时,logcat 说应用程序可能在主线程
按照 Google 使用服务的示例,我创建了几个这样的线程。我不能使用 IntentService,因为我正在做一些涉及等待回调的事情。 但是,我不知道如何终止以这种方式启动的线程。据我了解,线程会在
我试图了解使用 HandlerThread 的最佳用例。 根据定义: "Handy class for starting a new thread that has a looper. The loo
为什么安卓HandlerThread需要构造函数的名称? Public constructors HandlerThread(String name) HandlerThread(String nam
我基本上有描述的内存泄漏here由 LeakCanary 检测到。 我正在尝试通过使用上述帖子中的“管道工修复”来解决此问题,该帖子使用空消息刷新消息队列。每次空闲时提供的代码示例刷新消息队列。在我的
我有一个线程正在等待处理程序线程创建一个对象,它将初始化一个指向该对象的指针。我读到您无法同步指针的引用更改。我该如何去做呢? 我首先尝试让线程返回对象,但这仍然不起作用。这是我到目前为止所拥有的。“
我正在尝试学习如何使用 HandlerThread 在其自己的线程中运行 MediaPlayer。但我在第一步就失败了。我花了 2 天的时间痛苦地试图理解 Java/Android 中的线程,并且真的
这个错误让我困惑了几个小时。我收到了 NullPointerException。问题是这个错误不一致。它在我启动应用程序时发生,但只是偶尔发生。所以我不确定是什么原因造成的。 对于错误日志中的冗长问题
我想请人解释一下,HandlerThread 和 IntentService 之间的主要区别是什么,主要用例场景是什么? 我知道 HandlerThread 包含一个 Looper,它管理由 Hand
我的应用程序使用 HandlerThread 进行一些在需要在后台线程上运行的组件之间共享的操作。大部分时间该线程将处于等待状态。 我能否让这个 HandlerThread 在我的应用程序中运行(等待
我有一些代码使用单个 HandlerThread 和 Handler 向它发送消息。有没有办法用协程来做到这一点?我不想每次都创建新的协程,我只想在 HandlerThread 上执行代码块。请帮忙
我有一个名为“bluetoothIn”的处理程序,我想使用将提供循环程序的 HandlerThread 类将它传递给一个单独的循环程序。但是,我需要将结果从“handleMessage(Message
我想从 GUI 线程设置一个 HandlerThread。一段时间后,当在 GUI 上单击按钮时,它会运行 callHello(),然后将消息发送到位于非 GUI 线程上的 HelloLogger 对
我正在尝试了解 intentService 如何在单个后台线程中完成所有工作,如文档所述。所以我深入研究源代码,并有一个问题 public abstract class IntentServi
它们都循环遍历某个队列(消息/Intent )并执行它们? 最佳答案 IntentService 并不是真正循环 Intents 队列,它使用 Service 接收 Intents 并在工作线程中处理
我正在为 Android 修改 Google 的 Camera2 API 示例,可在此处找到:https://github.com/googlesamples/android-Camera2Basic
我是一名优秀的程序员,十分优秀!