Browse Source

App: Sending data in Async Task

master
Baran Yasar 1 year ago
parent
commit
cd6973efbb

+ 33
- 0
MobileApp/app/src/main/java/com/example/lfrmobileapp/DataTransferAsyncTask.java View File

package com.example.lfrmobileapp;

import android.os.AsyncTask;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

private class DataTransferAsyncTask extends AsyncTask<Void, Void, Void> {
private static final int sendRatePerSecond = 2;
private String hostname;
private int portNumber;
DataTransferAsyncTask(String hostname, int portNumber) {
this.hostname = hostname;
this.portNumber = portNumber;
}
@Override
protected Void doInBackground(Void... params) {
try {
Socket socket = new Socket(hostname, portNumber);
OutputStream outputStream = socket.getOutputStream();
while (!isCancelled()) {
outputStream.write("data".getBytes());
outputStream.flush();
Thread.sleep(1000 / sendRatePerSecond);
}
socket.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
}

Loading…
Cancel
Save