private class ReceiveDataTask extends AsyncTask<Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... voids) {
try {
// Establish a TCP connection with the server
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, 50523), 500);
while (screen_running) {
// Receive the screen image data
InputStream inputStream = socket.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
// Decode the image data into a Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
// Update the ImageView on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
if (bitmap != null)
{
control_screen_computer_screen.setImageBitmap(bitmap);
Log.wtf("receive data task", "image changed");
}
else
{
Log.wtf("receive data task", "image data null");
}
}
});
Thread.sleep(20);
}
// Close the connection
socket.close();
}
catch (Exception e)
{
Log.wtf("receive data task", "error occur " + e);
}
return null;
}
}