Skip to content

Instantly share code, notes, and snippets.

@flawnn
Created June 14, 2017 17:50
Show Gist options
  • Save flawnn/652f3f3462ad50ef4f116e3227effccd to your computer and use it in GitHub Desktop.
Save flawnn/652f3f3462ad50ef4f116e3227effccd to your computer and use it in GitHub Desktop.
ReceiveThread
package me.flawn.memecopyz;
import android.app.Activity;
import android.widget.TextView;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
// Thread wird in der Main nur einmal gestartet, nicht gestoppt
public class Receive implements Runnable
{
public Activity activity;
public Receive(Activity _activity) {
this.activity = _activity;
}
@Override
public void run() {
Socket server = null;
try {
server = new Socket("localhost", 3141);
Scanner in = new Scanner(server.getInputStream());
String text = in.nextLine();
//soll eigentlich in der App einen Text ersetzen
TextView myAwesomeTextView = (TextView)this.activity.findViewById(R.id.textView3);
myAwesomeTextView.setText(text);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (server != null)
try {
server.close();
} catch (IOException e) {
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment