简体   繁体   中英

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. I trying to get the dataarray back to the Bluetooth_dataDisplay extends AppCompatActivity and call it in the oncreate. How do I senddataarray back to main activity? Please help any expert. I had look at post that talked about thread data send back to main UI. But I am very new to it, so it quite confusing. :( Help please.

Replace handler call under your beginListenForData() function

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

with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM