繁体   English   中英

如何使用parcelable在活动之间传递线程类对象

[英]how to pass a thread class object from activity to activity using parcelable

好的,我正在为一些蓝牙应用程序制作一个应用程序,因此我曾经与其他设备连接过线程类,现在我想将该线程类实例传递给另一个活动,这样我就不必重新连接它,因此我实现了该线程类可打包,但不起作用,并引发NullPointer异常。

这是威胁类ClientThread.java:-

public class ClientThread extends Thread implements Runnable,Parcelable {

private BluetoothDevice device;
private final BluetoothSocket socket;
private Context context;
private BluetoothAdapter adapter;
private static String address = "14:36:05:7B:39:9B";//"98:D3:31:60:06:CA";
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private Handler handle;
protected InputStream is = null;
protected OutputStream os = null;
private boolean first;
private String password;

public ClientThread(Context context, Handler handle, String pass, boolean check) {
    this.context = context;
    adapter = BluetoothAdapter.getDefaultAdapter();
    this.password = pass;
    this.handle = handle;
    this.first = check;
    BluetoothSocket tmpSocket = null;

    try {
        device = adapter.getRemoteDevice(address);
        tmpSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    socket = tmpSocket;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {

        adapter.cancelDiscovery();

        socket.connect();

        handle.sendEmptyMessage(2);

        dataTransfer(socket);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        handle.sendEmptyMessage(3);
        e.printStackTrace();
        try {
            socket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

private boolean listen = true;

public void dataTransfer(BluetoothSocket soc) throws IOException {

    is = soc.getInputStream();
    os = soc.getOutputStream();

    byte[] buffer = new byte[256];
    int bytes;

    if (first) {
        send(("Validate" + password).getBytes());
        first = false;
    }

    while (listen) {
        try {
            bytes = is.read(buffer);
            handle.obtainMessage(1, bytes, -1, buffer).sendToTarget();
            // final String data = new String(buffer, 0, bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public void send(byte[] buffer) {
    try {
        os.write(buffer);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void cancel() {
    try {
        listen = false;
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

int mData=0;

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

}

public static final Parcelable.Creator<ClientThread> CREATOR = new Parcelable.Creator<ClientThread>() {
    public ClientThread createFromParcel(Parcel in) {
        return new ClientThread(in);
    }

    public ClientThread[] newArray(int size) {
        return new ClientThread[size];
    }
};

// example constructor that takes a Parcel and gives you an object populated with it's values
private ClientThread(Parcel in) {
    mData = in.readInt();
    socket=null;
}

}

请帮助我如何正确使其可包裹,以便我可以通过黑白活动

您无法通过Intent Extra传递Thread ,因为无法使Thread成为ParcelableSerializable 或者:

  • 这些不应该是单独的活动,而应该是一个具有变化的用户界面的活动(例如,通过片段); 要么

  • 此线程应由Service管理,尤其是即使用户从您的应用导航到其他地方,该线程也需要处于活动状态时; 要么

  • 此线程应由某个单例仔细管理,而不应由任何一个活动拥有

暂无
暂无

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

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