Browse Source

Dont use AsyncTask

master
Tim Zeuner 1 year ago
parent
commit
b361fad52d

+ 48
- 8
MobileApp/app/src/main/java/com/example/lfrmobileapp/ui/notifications/NotificationsFragment.java View File

package com.example.lfrmobileapp.ui.notifications; package com.example.lfrmobileapp.ui.notifications;


import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;


import androidx.annotation.NonNull; import androidx.annotation.NonNull;


import com.example.lfrmobileapp.Communication; import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.DataTransferAsyncTask; import com.example.lfrmobileapp.DataTransferAsyncTask;
import com.example.lfrmobileapp.R;
import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding; import com.example.lfrmobileapp.databinding.FragmentEinstellungenBinding;


import java.io.DataOutputStream;
import java.net.Socket;


/*class NetworkThread extends Thread {
String ip;
int port;
boolean connected;

NetworkThread(String ip, int port) {
this.ip = ip;
this.port = port;
this.connected = false;
}

public void run() {
try {
Socket socket = new Socket(ip, port);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes("Verbindung aufbauen");
outputStream.flush();
socket.close();
connected = true;

} catch (Exception e) {
e.printStackTrace();
connected = false;
}
}
}*/


public class NotificationsFragment extends Fragment { public class NotificationsFragment extends Fragment {


ipAddress = String.valueOf(binding.ipAdress.getText()); ipAddress = String.valueOf(binding.ipAdress.getText());
port = Integer.parseInt(String.valueOf(binding.port.getText())); port = Integer.parseInt(String.valueOf(binding.port.getText()));


dataTransferAsyncTask = new DataTransferAsyncTask(ipAddress, port);
dataTransferAsyncTask.writeTelegram("Verbindung aufbauen");
dataTransferAsyncTask.execute();

if(dataTransferAsyncTask.getConnectionState()){
new Thread(new Runnable(){
public void run(){
try {
Socket socket = new Socket(ipAddress, port);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeBytes("Verbindung aufbauen");
outputStream.flush();
socket.close();
connected = true;

} catch (Exception e) {
e.printStackTrace();
connected = false;
}
}
}).start();

if(connected){
editor.putString(ipKey, ipAddress); editor.putString(ipKey, ipAddress);
editor.putInt(portKey, port); editor.putInt(portKey, port);
editor.apply(); editor.apply();

Loading…
Cancel
Save