我已經從運行在同一活動中的線程得到了結果,但在更改活動後,我無法獲得該結果。有什麼方法可以得到這個結果嗎? 我在這裏的主題,我從runOnUiThread
如何從Android上的多個活動中獲取運行線程的結果?
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;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // 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
bytes = mmInStream.read(buffer);
final String s = new String(buffer);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), s.toString(),
Toast.LENGTH_SHORT).show();
txtcomand.setText(s);
intent.putExtra("ct", s);
}
});
// Send the obtained bytes to the UI activity
// .obtainMessage(MESSAGE_READ, bytes, -1, 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) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
使用AsyncTask而不是你的線程執行 – mgokgoz