简体   繁体   English

如何在多个活动中使用连接线程

[英]How to use Connected Thread in multiple activites

I am newbie in Android. 我是Android的新手。 First I've written an application which connects to another device via Bluetooth, then sends and receives data, using socket and connected thread. 首先,我编写了一个应用程序,该应用程序通过蓝牙连接到另一台设备,然后使用套接字和连接的线程发送和接收数据。 While using one activity everything works great, I receive data using handler. 使用一项活动时,一切工作正常,我使用处理程序接收数据。 Then I started to make an application with multiple activities, so I made a special class for socket connection and connected thread. 然后,我开始制作具有多个活动的应用程序,因此为套接字连接和连接线程创建了一个特殊的类。 I send data normally from any activity, but I don't know how to receive an answer (how to make a handler in many activities, or what alternative to use). 我通常从任何活动发送数据,但我不知道如何接收答案(如何在许多活动中创建处理程序,或使用什么替代方法)。 Could you possibly help me and write this lines of code, that I should add. 您能帮我写这行代码吗,我应该补充一下。

Thanks. 谢谢。

Here is my thread: 这是我的主题:

private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
          mmSocket = socket;
          InputStream tmpIn = null;
          OutputStream tmpOut = null;

          try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
          }
          catch (IOException e) {}

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
          }
          public void run() {
            byte[] buffer = new byte[256];
            int bytes;
            while(true) {
              try {
                bytes = mmInStream.read(buffer);

                byte[] readBuf = (byte[]) buffer;
                String strIncom = new String(readBuf, 0, bytes);

                  // Here I need some method to send data to activity
              }
              catch (IOException e) {
                  break;
                }
              }
            }

              public void write(String message) { 
            byte[] msgBuffer = message.getBytes();
                try {
                    mmOutStream.write(msgBuffer);
                } catch (IOException e) {

                  }

          }

} }

Well, one thing you can do is the use of an Interface , 好吧,您可以做的一件事就是使用Interface

for example: 例如:

public class ConnectedThread extends Thread {
// You declare your interface in the Class body
    public static interface CallBackListener
    {
        public void onReceived(String msg);
    }

   private final BluetoothSocket mmSocket;
   private final InputStream mmInStream;
   private final OutputStream mmOutStream;
   private CallBackListener listener = null;

// then, those who want to use this class must implement 
// this interface
   public ConnectedThread(BluetoothSocket socket, CallBackListener aListener) 
   {        
         this.listener = aListener;
         ... 

And in the run method of the thread you do like this: 在线程的run方法中,您可以这样做:

// Here I need some method to send data to activity
if (listener != null)
{
   listener.onReceived(strIncom);
}

And when you create the class, do like this: 创建类时,请执行以下操作:

BluetoothSocket socket = null;
/*
* Create the BlueToothSocket here
   BluetoothSocket socket = new BluetoothSocket(...);
*/

ConnectedThread conThread = new ConnectedThread(socket, new ConnectedThread.CallBackListener()
{
   @Override
   public void onReceived(String msg)
   {
    // here you'll receive the string msg
    // keep in mind that you receive this call 
    // in the ConnectedThread's context, not the UI thread
   }
});

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

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