sendMessageDelay()大家都明白,说是延时多少才发送。而sendMessageAtTime()意思也明白,那么怎么用呢?这里的关键,在于怎么取时间。正确做法是:
long mLastDisplayTime = 0; @Override protected void onNewIntent(Intent intent) { String text = intent.getStringExtra("message"); Message msg = mHandler.obtainMessage(MESSAGE_NEW_ARRIVED, text); # 正确取时间的代码。 long current = SystemClock.uptimeMillis(); if (mLastDisplayTime == 0) { mLastDisplayTime = current; } else { if (current > mLastDisplayTime+2000) { mLastDisplayTime = current; } else { mLastDisplayTime += 5000; } } mHandler.sendMessageAtTime(msg, mLastDisplayTime); }
从上述代码中可以看到,关键在于取时间的代码:SystemClock.uptimeMillis()