处理程序sendMessageDelayed仅在第一次工作

时间:2013-04-11 13:59:11

标签: android handler

我遇到Handler问题。它在第一次实例化时工作正常。

处理程序位于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);
        }
    };

几次后我跑

    mRedrawHandler = new RefreshHandler();
    mRedrawHandler.sleep();
瞬间调用

sendMessageDelayed。这篇文章中的问题似乎相似:

Handler.sendMessageDelayed(msg, delay) not working correctly

你有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

在sleep方法中,acquireMessage(0)的对象是什么? 这似乎是问题所在。您不能使用上次使用的消息对象。你必须创建新的消息对象。