1

I am having problems with a Handler. It works fine the first time I instantiate it only.

The handler is inside a View:

    private long delayMillis; 
    private long mMoveDelay = 1000;

    /**
     * Create a simple handler that we can use to cause animation to happen.  We
     * set ourselves as a target and we can use the sleep()
     * function to cause an update/invalidate to occur at a later date.
     */
    private RefreshHandler mRedrawHandler;


    class RefreshHandler extends Handler {

        // serve per far vedere tile 2 almeno un tempo prestabilito checkTime

        @Override
        public void handleMessage(Message msg) {


            delayMillis+=mMoveDelay;
            System.out.println("--->handleMessage mMoveDelay"+10000);
            System.out.println("--->handleMessage delayMillis"+delayMillis);


            sleep();
        }

        public void sleep() {

            //this.removeMessages(0);
            sendMessageDelayed(obtainMessage(0), mMoveDelay);
            System.out.println("--->sleep delayMillis"+delayMillis);
        }
    };

After a few times I run

    mRedrawHandler = new RefreshHandler();
    mRedrawHandler.sleep();

sendMessageDelayed is called instantaneously. The problem seems similar in this post:

Handler.sendMessageDelayed(msg, delay) not working correctly

Have you got any ideas? Thanks

Community
  • 1
  • 1
Gyonder
  • 3,674
  • 7
  • 32
  • 49

1 Answers1

0

In the sleep method what's the object of obtainMessage(0)? It seems to be the problem. You cannot use a message object which used last time. you've got to create new message object.