0

Hey,
I am a programming a lobby system for android device,
so that one device host a lobby for a game (i am programming) and the others can join the one.
I am currently at the point, at which one device asks the Host to join and the host saves that device in his players list and displays it at his screen and sends some information back.

That is the Async-Thread handeling the host stuff, so if it receives a message "Join-Host", it calls over the publishProgress-Method (so it is back on the Main Thread) a Method in my main class which than changes a Textfield inside the UI.


protected void onProgressUpdate(ArrayList<String>... values){
    if(msg.equals("Join-Host")){
        System.out.println("Gets Called");
        owner.add_Player(recvpacket.getAddress().toString());
    }

}

protected ArrayList<String> doInBackground(String... IP){
    DatagramSocket c = null;
    try{
        //Setup Datagram Server
        c = new DatagramSocket(8888, InetAddress.getByName("0.0.0.0"));
        c.setBroadcast(true);
        while(!cancel) {
            //Receive Packets
            byte[] recvBuffer = new byte[15000];
            recvpacket = new DatagramPacket(recvBuffer,recvBuffer.length);
            c.receive(recvpacket);
            //Verify Packet
            msg = new String(recvpacket.getData()).trim();
            System.out.println("Received Packet with message: " + msg);
            if(msg.equals("Find-Hosts")){
                byte[] sendData = (Build.MODEL).getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,recvpacket.getAddress(),recvpacket.getPort());
                c.send(sendPacket);
            }else{
                publishProgress(null);
            }




    }

As a result,for the first step, i would like to get the joining device directly displayed on the host device, after it receives the message.


So everthing works, but not immediately it takes until something else happens in the doInBackground Method to execute the command in the main class i called over the interface to display the new device.

Are there any suggestions how to solve this problem?
Thanks for your help.

1 Answers1

0

Android does not update its UI immediately. If you change an element, it might take some seconds or even longer until Android redraws its UI.

But you can force redrawing by calling the invalidate() method on the element (the View) you want to have redrawn. In your case this would be right after changing the element. Then your changes should be visible almost immediately depending on how busy your device is at that moment.

DeBukkIt
  • 86
  • 1
  • 7
  • That was also one of my thougths, but if i have a System.out.println command in the method before i change the UI stuff, that also doesn't get printed out until, a new packet receives at the socket, so i think the whole method does not get called before something is happening in doInBackground – Lord Imperator Dec 12 '17 at 17:36