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.