0

Im new to Android programming, my app is supposed create a socket in order to maintain constant contact with a server (get status and updates etc) while the user changes to different activities while using the app in general.

Im wondering how its possible for the user to change to different activity classes while still have a constant connection to the server.

I though maybe in main activity to create some background handler that always runs even if the player changes to a different activity.

That handler would be the entity that would maintain contact with the server, like a sort of background task, and also communicate status to show in whatever activity that happen to be running at the time.

something like

 OnCreate(....)
 {
                handler=new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                          tick();
                          handler.postDelayed(this,1000);
                    }
                },1000);
  }

 void tick()
  {
    communicate with server
  }

But what if the user changes to a different activity, does the handler still run? Can the socket still receive data from server even when a different activity is running if i set up some kind of socket call back that calls a function when it gets data, but that call back would be in the main activity, how can it get called if another activity is active?

Also how to communicate new information gotten from the socket to any activity that happens to be running as to update its UI. I would imagine having a data structure that all activities can access, that is populated by the thread or whatever is communicating with the server.

It seems pretty complicated, anybody have example or know the standard way to do it?

Thanks

Paul Man
  • 141
  • 1
  • 10

1 Answers1

0

This works for me, but I'm pretty sure there are better ways to keep the socket connection alive when changing activities, like using services(as I have read but not yet tried). You can have a look at an example of services here: Services Example

In my Main Activity (Where I create socket)

    public static Socket socket;

    public static   Socket getSocket(){
        return socket;
    }

    public static synchronized void setSocket(Socket socket){
        MainActivity.socket = socket;
    }

 public class ClientThread implements Runnable {

        @Override
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);
                setSocket(socket);
                if(socket!=null){
                    Intent goToNextActivity = new Intent(getApplicationContext(), SecondActivity.class);
                    startActivity(goToNextActivity);
                }

            } catch (UnknownHostException e1) {
                e1.printStackTrace();

            } catch (IOException e1) {
                e1.printStackTrace();

            }
        }

    }

In my second activity, I just call it like this to use the same socket I created (for example)

MainActivity.getSocket().getOutputStream().write(String.valueOf(multiplier).getBytes());

Works well for me!

mctjl_1997
  • 163
  • 1
  • 3
  • 17
  • Yeah, static variables are bad. Globally instances of an Activity are also... A Service would be preferred – OneCricketeer Jun 14 '17 at 03:04
  • Can i ask why static variables are bad? (Curious) Because their values can be easily changed? Yup i think services will be the best practice – mctjl_1997 Jun 14 '17 at 03:06
  • Let me rephrase, static variables in an Activity that carry any state can cause memory leaks. And the OS can kill your MainActivity at any point, therefore removing the Socket – OneCricketeer Jun 14 '17 at 03:42