Overwrite, reuse Messages (Handler)

1

When executing the following code:

@SuppressWarnings({"BusyWait"})
@Override
public void run() {
    Bundle datos = new Bundle();
    Message msg = new Message();
    for(Integer i = 0; i <= 10; i++){
        try {
            datos.putString("mensaje", i.toString());
            msg.setData(datos);

            handler.sendMessage(msg);
            Thread.sleep(2000);

            } catch (InterruptedException e) {
                   e.printStackTrace();
            }
        }
    }
}

after the first iteration I basically get this error:

  
    

java.lang.IllegalStateException:
    target = com.runner.handler.MainActivityHandler $ MyHandler
This message is already in use.

  

Which is quite logical.

I would like to know if there is any way to call the setData of the same Message object more than once Is it possible to reuse the same Message object, but with a different message, worth the redundancy?

    
asked by ViejoCadillac 18.10.2016 в 23:49
source

0 answers