1

I need to continously broadcast UDP packets from a Android application, so I created a class that derives from TimerTask. When I try to update a UI element from within my TimerTask, the app crashes which tells me that it runs on a separate thread.

However, when I'm trying to send a UDP packet, the app crashes because of a NetworkOnMainThreadException.

// simplified
public class UdpDiscoveryTask extends TimerTask {

    private final DatagramSocket _socket;

    public UdpDiscoverytAsk() {
        _socket = new DatagramSocket(PORT);
    }

    @Override
    public void run() {
        DatagramPacket packet = new DatagramPacket("Hello".getBytes().....);
        _socket.send(packet);
    }

}
xvdiff
  • 2,179
  • 2
  • 24
  • 47
  • `TimerTask` usually executes `run()` on a background thread IIRC. How are you using the `TimerTask`? Can you post the stack trace? Have you considered switching to `ScheduledExecutorService`? – CommonsWare Aug 24 '14 at 22:08
  • Post the stack trace. It probably indicates that you can't construct the DatagramSocket in the constructor. – user207421 Aug 24 '14 at 22:18

2 Answers2

1

Have you tried using an AsyncTask that get's called from a TimerTask repeatedly?

Taken from this question

public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {       
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {       
                    try {
                        PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
                        // PerformBackgroundTask this class is the class that extends AsynchTask 
                        performBackgroundTask.execute();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                    }
                }
            });
        }
    };
    timer.schedule(doAsynchronousTask, 0, 50000); //execute in every 50000 ms
}

By @Rasel

Community
  • 1
  • 1
Parth Mehrotra
  • 2,712
  • 1
  • 23
  • 31
  • I'm dumb as bread. I tried calling the `TimerTask` using `.run()` instead of with a `Timer`. It works now, but I prefer your `AsyncTask` approach. Thank you. – xvdiff Aug 24 '14 at 22:12
0
@override 
public void run() {

 new SendTask().exectue(null,null,null);
}



 class SendTask extends AsyncTask<Void,Void,Void> {


    public void doInBackground(Void.. params) {

         DatagramPacket packet = new DatagramPacket("Hello".getBytes().....);
    _socket.send(packet);

    }


}
Itzik Samara
  • 2,278
  • 1
  • 14
  • 18