簡體   English   中英

如何在處理程序中使用Inputstream

[英]How to use Inputstream with Handler

我不明白我應該如何使用Inputstream和Handler。

我希望有人能解釋一下。 我閱讀了一些教程,但我理解它們的作用,但不了解它們的作用。

這是我不明白的例子:

   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和將其存儲在byte[]稱為myBuffer 它不是從緩沖區讀取。

此處查看InputStream的文檔,或者:

從輸入流中讀取一定數量的字節,並將其存儲到緩沖區數組b中。

其次:

該代碼的作用(沒有看到其余的代碼)是,它開始在后台線程上讀取輸入inputstream 每次將在此處讀取一個字節:

ret = mInputStream.read(buffer);

為了能夠從后台線程更改視圖,我們將需要某種機制在后台線程和UI線程之間建立橋梁,因為只有UI線程才能更改視圖。

進來的處理程序 :-)

引用從Handler文檔:

[...]使要在與您自己的線程不同的線程上執行的操作排隊。

因此,在您的代碼中,您將“獲取”與您的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中顯示該消息。

Handlers在某種程度上是一個復雜的主題,但是在許多地方都需要Handlers ,當您開始使用多個線程並且大多數應用程序有時會開始使用多個線程時:-)

在旁注

可以使用其他東西,但可以使用處理程序。

在while循環內,它將message到UI Handler您可以使用名為runOnUiThread(runnable)的便捷方法替換Handler程序代碼,以便您的代碼如下所示:

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方法綁定到Activity類。 另外,某些字段可能必須是final字段才能從runOnUiThread方法讀取它們,但我希望您明白這一點:-)

希望這會有所幫助-否則讓我知道,我將盡力闡述:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM