EDITORIAL
カウントダウン中に、これらの日は、合計と関連する機能を実現するために、CountDownTimerを実現使います。しかし、カウントダウンがしばしば秒ジャンプ発見されたことがわかっテストする場合、1ケースは表示されませんので、この点でいくつか理解していました。この記事では、正確な理由CountDownTimer、および独自の実現のタイマーにカウントダウンを導入する準備ができていません -
CountDownTimer問題
うるう秒の発見後、質問1(ここでログを保存することを忘れて、それを指示する)プリントアウトして、最初に各onTick()コールバック時間は表示されません。私たちは、より多くの、実際の試験では、主に1000ミリ秒までの間隔ためにオンライン検索で問題が発生しますが、コールバック時間ごとに数ミリ秒、またはパーセント10ミリ秒以上、私の機器40msの、すなわち、すべてのカウントダウン25S、1秒の表示をスキップします。
ソースコードは、オンライン分析にも多くのことを非常に単純な把握されて最初に私たちはCountDownTimerを見て、言っても過言ではありません。投稿キーコンポーネント:
synchronized (CountDownTimer.this) {
if (mCancelled) {
return;
}
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
if (millisLeft <= 0) {
onFinish();
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
long lastTickDuration = SystemClock.elapsedRealtime() - lastTickStart;
long delay;
if (millisLeft < mCountdownInterval) {
// just delay until done
delay = millisLeft - lastTickDuration;
// special case: user's onTick took more than interval to
// complete, trigger onFinish without delay
if (delay < 0) delay = 0;
} else {
delay = mCountdownInterval - lastTickDuration;
// special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
}
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
コード遅延の計算上の計算が実行時間遅れonTick後、すなわち、来るonTick実行するために、使用前と後の時間である。sendMessageDelayedが、この部分は、データを読み、onTick判決の前と後を含め、ミリ秒の精度を保証するものではありません。時間のかかるはカウントされません。異なるモデルが異なる時間遅延が生じ、さまざまな方法を使用しているため。
カスタムカウントダウン
ハンドラによって実装される実装とCountDownTimer、。ビジネスニーズので、直接秒のカウントダウン剥離ビジネス・ロジック、コードステッカーを作ります。
主なアイデア:遅延を計算するためのメッセージを送信する前に計算に行くために。
計算値:開始時の記録時間lastMills(MS)は、各送信されたメッセージの後に、lastMills 1000をインクリメントし、各メッセージを送信する前に現在の時刻を取得し、計算し、次のメッセージの到着予定時刻との差、そのメッセージは、この時間遅延を伝達され、
class MyHandler extends Handler {
private int totalSeconds;
private long lastMills = 0L;
MyHandler(int totalSeconds) {
super();
this.totalSeconds = totalSeconds;
}
public void start() {
Message msg = obtainMessage();
sendMessage(msg);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
boolean isContinue = true;
if (totalSeconds > 0) {
// do sth when tick
} else {
isContinue = false;
}
if (isContinue) {
totalSeconds --;
long internal = 1000;
long now = System.currentTimeMillis();
// print time
Log.i(TAG, "handleMessage: now=" + now);
if (lastMills != 0L) {
internal = internal - (now - lastMills);
lastMills += 1000;
} else {
lastMills = now + 1000;
}
Message message = obtainMessage();
sendMessageDelayed(message, internal);
} else {
finish();
}
}
private void finish() {
// do things when finish
}
}
毎回プリントアウトし、エラーが数ミリ秒の変動の範囲内で、かつ蓄積しない、あなたは全体の数であれば破壊された変動を使用した場合、丸めは、好ましくは増加しています。
Johnny Deng : 08-18 13:32:47.894 handleMessage: now=1566106367894
Johnny Deng : 08-18 13:32:48.894 handleMessage: now=1566106368894
Johnny Deng : 08-18 13:32:49.894 handleMessage: now=1566106369894
Johnny Deng : 08-18 13:32:50.896 handleMessage: now=1566106370896
Johnny Deng : 08-18 13:32:51.895 handleMessage: now=1566106371895
Johnny Deng : 08-18 13:32:52.897 handleMessage: now=1566106372896
Johnny Deng : 08-18 13:32:53.896 handleMessage: now=1566106373896
Johnny Deng : 08-18 13:32:54.894 handleMessage: now=1566106374894
Johnny Deng : 08-18 13:32:55.896 handleMessage: now=1566106375896
Johnny Deng : 08-18 13:32:56.894 handleMessage: now=1566106376894
Johnny Deng : 08-18 13:32:57.895 handleMessage: now=1566106377895
Johnny Deng : 08-18 13:32:58.893 handleMessage: now=1566106378893
Johnny Deng : 08-18 13:32:59.881 handleMessage: now=1566106379881
Johnny Deng : 08-18 13:33:00.901 handleMessage: now=1566106380901
Johnny Deng : 08-18 13:33:01.893 handleMessage: now=1566106381893
Johnny Deng : 08-18 13:33:02.894 handleMessage: now=1566106382894
Johnny Deng : 08-18 13:33:03.895 handleMessage: now=1566106383895
Johnny Deng : 08-18 13:33:04.895 handleMessage: now=1566106384895
Johnny Deng : 08-18 13:33:05.895 handleMessage: now=1566106385894
Johnny Deng : 08-18 13:33:06.895 handleMessage: now=1566106386895
Johnny Deng : 08-18 13:33:07.894 handleMessage: now=1566106387894
Johnny Deng : 08-18 13:33:08.897 handleMessage: now=1566106388897
Johnny Deng : 08-18 13:33:09.897 handleMessage: now=1566106389897
Johnny Deng : 08-18 13:33:10.893 handleMessage: now=1566106390893
Johnny Deng : 08-18 13:33:11.893 handleMessage: now=1566106391893
Johnny Deng : 08-18 13:33:12.896 handleMessage: now=1566106392896
Johnny Deng : 08-18 13:33:13.895 handleMessage: now=1566106393895
Johnny Deng : 08-18 13:33:14.895 handleMessage: now=1566106394895
Johnny Deng : 08-18 13:33:15.894 handleMessage: now=1566106395894
Johnny Deng : 08-18 13:33:16.898 handleMessage: now=1566106396898
あなたがこの記事で何か問題を見つけた場合、私はあなたが、私は変更を加えることを指摘することを願って、この記事では賞賛のポイントにあなたには、いくつかのトラブルの大物を持って支援する場合〜