如何使用多个连接activites主题 [英] How to use Connected Thread in multiple activites

查看:98
本文介绍了如何使用多个连接activites主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新手。首先,我已经写它通过蓝牙连接到另一台设备,然后发送和接收数据,采用socket连接线程的应用程序。当使用一个活动一切都很正常,我收到使用处理器的数据。
然后,我开始做多活动的应用程序,所以我做了套接字连接和连接线的特殊类。我通常从任何活动发送数据,但我不知道如何接受一个答案(如何让许多活动的处理程序,或者是替代使用)。你能不能帮我写code这行,我要补充。

I am newbie in 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.

感谢。

下面是我的主题:

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,

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

而在线程的run方法,你这样做:

And in the run method of the thread you do like this:

// 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
   }
});

这篇关于如何使用多个连接activites主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆