java线程如何与活动进行通信

时间:2014-12-15 20:36:30

标签: java android multithreading

Android提供asynctask和处理程序与活动进行通信。但普通的java线程是否可以与活动进行通信并更新textview?

我试着写一个基本的线程t = new thread();但它不允许我访问任何文本视图或任何活动成员

3 个答案:

答案 0 :(得分:3)

您可以使用Thread,但为了进行UI更新,它们必须位于应用程序的主线程上。 AsyncTask是一种为您管理此方法的方法,但您不必使用它。您可以通过RunnableActivity.runOnUiThread()个对象发布到UI线程,或者甚至在主线程的上下文中创建Handler并发布Runnable个对象或向其发送消息。

答案 1 :(得分:1)

只要您有权访问上下文:

context.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // put your UI code here
    }
});

答案 2 :(得分:0)

是,

这是一个蓝牙线程的示例,读取传入的字节流,然后使用消息处理程序更新各种Textview。

   public void run() {
    byte[] buffer; // buffer store for the stream
        int bytes; // bytes returned from read()
        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                buffer = new byte[4];
                    mmOutStream.write(253);
                    bytes = mmInStream.read(buffer,0,1);
                    mmOutStream.write(254);
                    bytes = mmInStream.read(buffer,1,1);
                    mmOutStream.write(255);
                    bytes = mmInStream.read(buffer,2,1);
                    mmOutStream.write(252);
                    bytes = mmInStream.read(buffer,3,1);
                    // A message handler to deal with updating the text views
                    mHandler.obtainMessage(MESSAGE_READ, buffer).sendToTarget();
                }
                catch (IOException e) {
                    break;
             }
        }
     }
    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }
}


Handler mHandler = new Handler(){           
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            switch(msg.what){
                case SUCCESS_CONNECT:
                    // Do Something;
                    Toast.makeText(getActivity(),"CONNECTED",Toast.LENGTH_SHORT).show();
                    /*
                     * For loop for test values
                     */
                    connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
                    listView.setVisibility(View.GONE);
                    connectedThread.start();
                    break;

                case MESSAGE_READ:
                    byte[] readBuf = (byte[])msg.obj;
                    int tempInt = byteToInt(readBuf[0]);
                    int speedInt = byteToInt(readBuf[1]);
                    int cadenceInt = byteToInt(readBuf[2]);
                    int distanceInt = byteToInt(readBuf[3]);
                    EditText temperatureData = (EditText)getActivity().findViewById(R.id.temperatureData);
                    temperatureData.setText(Integer.toString(tempInt) + " C" );
                    EditText cadenceData = (EditText)getActivity().findViewById(R.id.cadence);
                    cadenceData.setText(Integer.toString(cadenceInt) + " rpm");
                    EditText speedData = (EditText)getActivity().findViewById(R.id.speed_data);
                    speedData.setText(Integer.toString(speedInt) + " kph");
                    EditText distanceData = (EditText)getActivity().findViewById(R.id.distanceCovered);
                    distanceData.setText(Double.toString(distanceMethod(distanceInt)) + " km");
            }
        }       
    };

在上面的例子中,各种EditTexts通过handler更新。处理程序是各种各样的" switch"喜欢条件并运行与条件相关的代码。