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

@@ -1,6 +1,5 @@
package com.example.lfrmobileapp.ui.notifications;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
@@ -8,7 +7,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
@@ -17,9 +15,38 @@ import androidx.lifecycle.ViewModelProvider;

import com.example.lfrmobileapp.Communication;
import com.example.lfrmobileapp.DataTransferAsyncTask;
import com.example.lfrmobileapp.R;
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 {

@@ -70,11 +97,24 @@ public class NotificationsFragment extends Fragment {
ipAddress = String.valueOf(binding.ipAdress.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.putInt(portKey, port);
editor.apply();

Loading…
Cancel
Save