从处理程序更新UI

时间:2019-04-12 14:34:24

标签: android android-handler

我正在使用处理程序与其他负责蓝牙通信的线程进行通信。建立通信后,我想发送字节数组列表并显示进度。

Handler handler = new Handler(Looper.getMainLooper()) {
                        @Override
                        public void handleMessage(Message inputMessage) {
                            switch (inputMessage.what) {
                                case CONNECTION_STATE:
                                    connected = (boolean) inputMessage.obj;
                                    Log.d(TAG, "connected= " + String.valueOf(connected));
                                    if (connected) {
                                        for (int i = 0; i < data2send.size(); i++) {
                                            if (!isCancelled) {
                                                isPrinting = true;
                                                printButton.setText(getString(R.string.cancel));
                                                byte[] bytes2send = new byte[data2send.get(i).getDataLength()];
                                                System.arraycopy(data2send.get(i).getData(), 0, bytes2send, 0, data2send.get(i).getDataLength());
                                                try {
                                                    int progress = (i+1) * 100 / data2send.size();
                                                    Log.d(TAG, "onProgressUpdate: progress = " + progress + " %");
                                                    String statusString = getString(R.string.sending_page) + String.valueOf(i+1) + getString(R.string.of) + String.valueOf(data2send.size());
                                                    Log.d(TAG, statusString);
                                                    status.setText(statusString);
                                                    progressBar.setProgress(progress);
                                                    connectThread.write(bytes2send);
                                                } catch (Exception e) {
                                                    Log.e(TAG, "connection error");
                                                }
                                            } else {
                                                isPrinting = false;
                                                printButton.setText(getString(R.string.print_caps));
                                                break;
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                    };
                    connectThread = new ConnectThread(printer, handler);
                    connectThread.start();
                }

发送数据时,TextView内容和ProgressBar都无法更新,但是日志记录有效(log.d)。

1 个答案:

答案 0 :(得分:0)

在您的Activity中新建一个void

private void updateUI(final String statusString, final int progress) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            status.setText(statusString);
            progressBar.setProgress(progress);            
        }
    });

}

然后,在您的处理程序内

 try {
     int progress = (i+1) * 100 / data2send.size();
     Log.d(TAG, "onProgressUpdate: progress = " + progress + " %");
     String statusString = getString(R.string.sending_page) + String.valueOf(i+1) + getString(R.string.of) + String.valueOf(data2send.size());
     Log.d(TAG, statusString);
     updateUI(statusString, progress);
    ...
    ...