0

I've read a lot on this subject but I can't seem to find some answer that could help me. I've modeled my application as frontend-backend. The backend is just a server that waits for incoming connections. The problem is I start the server as soon as the app starts and I no longer communicate with it. Now I need the server to communicate with the frontend telling it someone connected. I tryed using static methods but I get an error from being unable to update de UI from a different thead. How can I proceed? EDIT:

My server class

public class Server {
public static int uniqueID;
private final int port;
private final Boolean keepWorking;
private final String username;

public Server(int port, String username) {
    this.port = port;
    this.username = username;
    keepWorking = true;

}
public void Start() {
    try {
        ServerSocket serverSocket = new ServerSocket(port);
        while (keepWorking) {

            Socket socket = serverSocket.accept();


            MainActivity.SomeoneConnected();
        }
        serverSocket.close();

    } catch (IOException e) {
        Log.d("Error", e.getMessage().toString());

    }

}

}

That's everything I need. The server to tell the frontend someone connected

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Please modify your question to explain precisely what sorts of Android constructs you are using. For example, is this "just a server" some sort of `Service`? What sorts of "connections" is it waiting for? – CommonsWare Jul 04 '13 at 21:58

3 Answers3

1

You should look into AsyncTasks:

http://developer.android.com/reference/android/os/AsyncTask.html

or any sort of thread/handler implementaion. -There are plenty of examples of this online

Also consider the ruOnUiThread() method of Activity.

All of these solutions are assuming your client/server architecute is contained within the same activity.

** Based on your edit:

you should either call MainActivity.SomeoneConnected();using an instance of your activity with instance.runOnUiThread() or create a handler on the Ui thread and post a runnable that calls MainActivity.SomeoneConnected(); from within the run() method using handlerInstance.post(yourRunnable)

here is an example of how to create a Runnable:

How to run a Runnable thread in Android?

Community
  • 1
  • 1
Scott
  • 1,652
  • 1
  • 13
  • 10
  • How can I get an instance of my activity in the backend? – Cristian Eduardo Lehuede Lyon Jul 04 '13 at 22:12
  • Sorry, I am a bit confused now. I was assuming that what you called a "fronend-backend" was actually all on the same app. Is this the case? Or have you posted code that is not in your app? Specifically, is the Server class part of your app? – Scott Jul 04 '13 at 22:14
  • Yes, they are all in the same app. It's just that the server is running on a different thread. Sorry that I've failed to make myself clear. It's just I call frontend and backend the differents packages of my app but they are all in the same app. – Cristian Eduardo Lehuede Lyon Jul 04 '13 at 22:17
  • 1
    Okay then, just add an instance variable of type Activity to the Server class and pass it throught the constructor of the Server class (for example: pass 'this' if creating the Server object from your activity class, pass `getActivity()` if creating the object with a fragment, ect...) – Scott Jul 04 '13 at 22:21
  • 1
    Or another option is to create a Handler (this must created on the ui thread) and create an instance variable of type Handler in the Server class and pass the handler through the Server constructor and then follow the runnable example – Scott Jul 04 '13 at 22:23
  • Never thought I could send activities as parameters. Thanks! Now it's working like a charm – Cristian Eduardo Lehuede Lyon Jul 04 '13 at 22:24
0

I highly recommend you use Google app engine and google cloud messaging to do this, it has great support for Android and means you don't have to poll your server

Read more: http://developer.android.com/google/gcm/index.html

Ryan S
  • 4,549
  • 2
  • 21
  • 33
0

The tool of choice for communication between Threads is the Handler.

Johannes
  • 31
  • 2