java - 如何将输入流与处理程序一起使用

标签 java android

我不明白应该如何使用输入流和处理程序。

希望有人能解释一下。我读了一些教程,我了解他们做了什么,但不知道他们是如何做的。

这是一个我不明白的例子:

   public void run() {
    int ret = 0;
    byte[] buffer = new byte[16384]; 
    int i;

    while (true) { // read data
        try {
            ret = mInputStream.read(buffer);
        } catch (IOException e) {
            break;
        }

        i = 0;
        while (i < ret) {
            int len = ret - i;
            if (len >= 1) {
                Message m = Message.obtain(mHandler);
                int value = (int)buffer[i];
                // &squot;f&squot; is the flag, use for your own logic
                // value is the value from the arduino
                m.obj = new ValueMsg(&squot;f&squot;, value);
                mHandler.sendMessage(m);
            }
            i += 1; // number of bytes sent from arduino
        }

    }
   }
    Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        ValueMsg t = (ValueMsg) msg.obj;
        // this is where you handle the data you sent. You get it by calling the      getReading() function
        mResponseField.setText("Flag: "+t.getFlag()+"; Reading: "+t.getReading()+"; Date: "+(new Date().toString()));
    }
};

对不起我的英语

最佳答案

好的,我会尽力在问题和评论中回答您的问题。

首先:使用 byte[]InputStream 上调用 read() 会将字节读入缓冲区。

byte[] myBuffer = new byte[16384];
myInputStream.read(myBuffer);

此代码将从 inputstream 读取字节并将其存储在名为 myBufferbyte[] 中。它不是从缓冲区读取。

查看 InputStream 的文档 here ,或者:

Reads some number of bytes from the input stream and stores them into the buffer array b.

第二:

代码的作用(看不到代码的其余部分)是它开始在后台线程上读取inputstream。它每次都会读取一个字节:

ret = mInputStream.read(buffer);

为了能够从后台线程更改 View ,我们需要某种机制在后台线程和 UI 线程之间建立桥梁,因为只有 UI 线程可以更改 View 。

出现 Handler :-)

引用自Handler文档:

[...] to enqueue an action to be performed on a different thread than your own.

在您的代码中,您因此“获取”与您的 UI 线程关联的当前 Handler:

Message m = Message.obtain(mHandler); // Obtain the UI thread handler.
int value = (int)buffer[i]; // Read data.
m.obj = new ValueMsg(&squot;f&squot;, value); // Create a message to send to the UI thread handler.
mHandler.sendMessage(m); // Send the message to the UI thread handler.

之后,UI 线程 HandlerhandleMessage 方法中接收消息:

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        ValueMsg t = (ValueMsg) msg.obj;
        // this is where you handle the data you sent. You get it by calling the      getReading() function
        mResponseField.setText("Flag: "+t.getFlag()+"; Reading: "+t.getReading()+"; Date: "+(new Date().toString()));
    }

并根据收到的消息采取行动,并在您选择的 TextView 中显示该消息。

处理程序 是一个复杂的主题,但在很多地方都需要,当您开始使用多线程时,大多数应用程序都会在某个时候开始使用多线程:-)

旁注

除了处理程序之外,还可以使用其他东西。

在 while 循环中,将消息发送到 UI Handler,您可以将 Handler 代码替换为名为 runOnUiThread(runnable) 所以你的代码可能看起来像这样:

while (i < ret) {
    int len = ret - i;
    if (len >= 1) {
        runOnUiThread(new Runnable() {
            int value = (int)buffer[i];
            mResponseField.setText(String.valueOf(value));                   
        });
    }
    i += 1; // number of bytes sent from arduino
}

请记住,上面的代码可能无法编译(我自己还没有尝试过......)。您必须位于 ActivityFragment 中,因为 runOnUiThread 方法绑定(bind)到 Activity 类。另外,有些字段可能必须是 final 才能从 runOnUiThread 方法读取它们,但我希望您明白这一点:-)

希望这有帮助 - 否则请告诉我,我会尽力详细说明:-)

关于java - 如何将输入流与处理程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267660/

相关文章:

android - 为什么使用用例? Android Jetpack 没有在文档中提及用例

java - 在循环错误中调用方法

java - Swagger UI 空白 PDF 下载

Android 中的 Java 7/8?

android - 如何从 gradle 导入没有 "compile: "的 kryonet 库?

java - Android将度数符号设置为Textview

java - 如何从jess中的java类中读取变量?

java - 使用动态文件名将文件从 FtpOutBoundGateway 移动到另一个远程位置

Java GRIB 解码器 : Extract data from GRIB2 files

android - DrawableLeft 与文本居中