UDP Sending via android cannot be automatic-ThrowExceptions

Exception or error:

I can send udp and received udp, the problem that I had face now is when i need to automatic sending data from client to server(to prove that the client is alive), the the server cannot received the data. but when I send the alive status via Button event. it worked.
here’s my code

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        establishConnection();
        /* Working */
        Button send = new Button(this);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 new Thread(new Runnable() {
            @Override
            public void run() {
                DatagramPacket receivePacket = null;
                byte[] command = (room+":Alive").getBytes();
                try {
                    receivePacket = new DatagramPacket(command, command.length, InetAddress.getByName(UrlColletion.hostAddress), 8084);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                java.lang.System.out.println("Sending");

                try {
                    serverSocket1.send(receivePacket);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
            }
        });
        /* Not Working*/
        final Handler handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    DatagramPacket receivePacket = null;
                    byte[] command = (room + ":Alive").getBytes();
                    try {
                        receivePacket = new DatagramPacket(command, command.length, InetAddress.getByName(UrlColletion.hostAddress), 8084);
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    java.lang.System.out.println("Sending");

                    try {
                        serverSocket1.send(receivePacket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                } finally {
                    //also call the same runnable to call it at regular interval
                    handler.postDelayed(this, 5000);
                }
            }
        };
        handler.post(runnable);
        setContentView(send);
    }
    DatagramSocket serverSocket1;
    void establishConnection(){
        try {
            serverSocket1 = new DatagramSocket(7070);
            serverSocket1.setReuseAddress(true);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean isOpen = true;
                while(isOpen) {
                    byte []message = new byte[1024];
                    DatagramPacket receivePacket = new DatagramPacket(message, 1024);
                    try {
                        serverSocket1.receive(receivePacket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String text = new String(message, 0, receivePacket.getLength());
                    System.out.println(text);
                }
                serverSocket1.close();
            }
        }).start();
    }

the code above is tested. when it send via handler, the server cannot received any data, while button, it can.
Additional information, the code is working on emulator, but when it comes on phone, the problem comes out. so it means on emulator the udp is sending successfully, but on phone, no.

How to solve:

Try to disable battery optimization or maybe implement a wakelock.
https://developer.android.com/training/scheduling/wakelock

Intent intent = new Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(intent);

###

Don’t know why but i just an thread on that.

final Handler handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            DatagramPacket receivePacket = null;
                            byte[] command = (room + ":Alive").getBytes();
                            try {
                                receivePacket = new DatagramPacket(command, command.length, InetAddress.getByName(UrlColletion.hostAddress), 8084);
                            } catch (UnknownHostException e) {
                                e.printStackTrace();
                            }
                            java.lang.System.out.println("Sending");

                            try {
                                serverSocket1.send(receivePacket);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();
                } catch (Exception e) {
                    // TODO: handle exception
                } finally {
                    //also call the same runnable to call it at regular interval
                    handler.postDelayed(this, 5000);
                }
            }
        };
        handler.post(runnable);

Leave a Reply

Your email address will not be published. Required fields are marked *