gpt4 book ai didi

java - 如何使用parcelable将线程类对象从一个 Activity 传递到另一个 Activity

转载 作者:行者123 更新时间:2023-12-01 18:05:38 27 4
gpt4 key购买 nike

好吧,我正在为一些蓝牙工作制作一个应用程序,所以有一个线程类,我用来与其他设备连接,现在我想将该线程类实例传递给另一个 Activity ,这样我就不必重新连接它,因此我实现了它线程类作为 Parcelable 但它不起作用并抛出 NullPointer 异常。

Here is that threa class 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;
}

}

Please help me how to correctly make it parcelable so that i could pass b/w activities

最佳答案

您无法通过 Intent 额外传递 Thread,因为 Thread 无法成为 可解析可序列化。要么:

  • 这些不应该是单独的 Activity ,而只是一个具有不断变化的 UI 的 Activity (例如,通过 fragment );或

  • 此线程应由 Service 管理,特别是如果即使用户从您的应用导航到其他应用程序,该线程也需要处于 Activity 状态;或

  • 该线程应该由某个单例仔细管理,并且不属于任何一个 Activity

关于java - 如何使用parcelable将线程类对象从一个 Activity 传递到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36652988/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com