简体   繁体   English

如何将数据从线程发送回主UI?

[英]How do I send data from thread back to main UI?

void beginListenForData() {
            //final Handler handler = new Handler();
            final Handler handler = new Handler(Looper.getMainLooper());
            final byte delimiter = 10; //This is the ASCII code for a newline character

            stopWorker = false;
            readBufferPosition = 0;
            readBuffer = new byte[2048];
            workerThread = new Thread(new Runnable() {
                public void run() {
                    while (!Thread.currentThread().isInterrupted() && !stopWorker) {
                        try {
                            int bytesAvailable = mmInputStream.available();
                            if (bytesAvailable > 0) {
                                byte[] packetBytes = new byte[bytesAvailable];
                                mmInputStream.read(packetBytes);
                                for (int i = 0; i < bytesAvailable; i++) {
                                    byte b = packetBytes[i];
                                    if (b == delimiter) {
                                        byte[] encodedBytes = new byte[readBufferPosition];
                                        System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                        final String data = new String(encodedBytes, "US-ASCII");
                                        readBufferPosition = 0;


                                                                              handler.post(new Runnable() {

                                            public void run() {

                                                //myLabel.setText(data);
                                                dataArray = new String []{data};
                                                //Log.d("dataArray", data);
                                            }
                                        });

                                    } else {
                                        readBuffer[readBufferPosition++] = b;
                                    }
                                }
                            }
                        } catch (IOException ex) {
                            stopWorker = true;
                        }
                    }
                }
            });


public class Bluetooth_dataDisplay extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        String MAC = getIntent().getStringExtra("MAC");
        mAdapter = BluetoothAdapter.getDefaultAdapter(); 
        BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);
        // Initiate a connection request in a separate thread
        ConnectingThread t = new ConnectingThread(bluetoothDevice);
        t.start();

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new RecyclerAdapter(dataArray);
        recyclerView.setAdapter(adapter);

    }

Begindata() is call in the connected thread. 在连接的线程中调用Begindata()。 I trying to get the dataarray back to the Bluetooth_dataDisplay extends AppCompatActivity and call it in the oncreate. 我试图将数据数组返回到Bluetooth_dataDisplay扩展AppCompatActivity并在oncreate中调用它。 How do I senddataarray back to main activity? 如何将dataarray发送回主要活动? Please help any expert. 请帮助任何专家。 I had look at post that talked about thread data send back to main UI. 我看过一篇有关将线程数据发送回主UI的文章。 But I am very new to it, so it quite confusing. 但是我对此很陌生,因此非常混乱。 :( Help please. :( 请帮助。

Replace handler call under your beginListenForData() function beginListenForData()函数下替换处理程序调用

handler.post(new Runnable() {
      public void run() {
        //myLabel.setText(data);
        dataArray = new String[]{data};
         //Log.d("dataArray", data);
      }
   });

with runOnUiThread() runOnUiThread()

 runOnUiThread(new Runnable() {
     @Override
     public void run() {
          //myLabel.setText(data);
          dataArray = new String[]{data};
          //Log.d("dataArray", data);
        }
   });

Everything under runOnUiThread() runs on UI/Main thread. runOnUiThread()下的所有内容都在UI / Main线程上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从线程获取数据(和事件)到主服务(UI)线程的事件循环? - How do I get data (and an event) from a thread into the main service (UI) thread's event loop? Android将数据从主UI线程发送到另一个线程 - Android send data from main UI thread to another thread 将数据从不同的类发送到主UI-Thread - Send data from a different Class to the main UI-Thread Android,如何将消息发送回主线程 - Android, How to send a message back to main thread 使用Android,如何将控制权传递回主线程? - With Android, how do I pass control back to the main thread? 如何从AsyncTask中的onPostExecute发回数据? - how do i send data back from onPostExecute in an AsyncTask? 将数据从UI线程发送到主活动“ Android”未在其中创建的另一个线程 - send data from the UI thread to another thread which is not created in by the Main Activity “Android” Android /套接字-如何从主UI线程向套接字线程发送信息? - Android/Sockets - How to send information from main UI thread to socket thread? 如何将数据从 ViewModel 内的线程发送到主 Activity/Fragment? - How to send data from thread inside a ViewModel to main Activity/Fragment? 如何将数据从线程发送到主要活动 - how to send data from thread to main activity java android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM