我要重新开始倒计时新的时候,倒计时结束。我给下面code:
futureInMillis = NEWTIME();
CountDownTimer remainingTimeCounter =新CountDownTimer(futureInMillis,1000){
公共无效onTick(长millisUntilFinished){
remainingTime = calculateRemainingTime(millisUntilFinished / 1000);
runOnUiThread(录入);
}
公共无效onFinish(){
// TODO:重新启动计数器
取消();
futureInMillis = NEWTIME();
// remainingTimeCounter = NULL;
this.start();
}
}。开始();
解决方案
我们应该知道 NEWTIME()
做的,你不要有什么要调用的方法取消()
,因为计时器是在 onFinish()
的方法,这意味着计时器结束倒计时
CountDownTimer remainingTimeCounter =新CountDownTimer(futureInMillis,1000){
公共无效onTick(长millisUntilFinished){
remainingTime = calculateRemainingTime(millisUntilFinished / 1000);
runOnUiThread(录入);
}
公共无效onFinish(){
// TODO:重新启动计数器
//取消(); //没有必要呼叫的取消这里()方法
futureInMillis = NEWTIME();
// remainingTimeCounter = NULL;
this.start();
}
}。开始();
编辑:
有类中的 CountDownTimer
来使用,以改变futureInMillis没有attribut,
如果你想使用,你应该使用两个 CountDownTimer
;你应该创建两个班,每个班都有一个定时器和futureInMillis的attribut,然后在第一类的计时器完成后,实例化第二课堂,通过futureInMillis给它,然后开始她的定时器,反之亦然。
**编辑2:**
或者干脆编辑从源头code安卓类CountDownTimer的code源代码并添加getter和setter方法为attributs futureInMillis是这样的:
/ *
*版权所有(C)2008年Android开源项目
*
* Apache许可证下授权,版本2.0(以下简称许可证);
*您可能不能使用这个文件除了在遵守许可。
*您可以在获得许可证的副本
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*许可证下发布分布在原样的基础上,
*无担保或任何形式的条件,无论是EX preSS或暗示的保证。
*请参阅许可的特定语言的管理权限和
*许可下限制。
* /
//此处添加您包的声明
进口android.util.Log;
/ **
*安排一个倒计时,直到在将来的时间,用
*沿途间隔定期通知。
*
*显示在文本字段30秒倒计时的例子:
*
*< pre类=prettyprint>
*新的CountdownTimer(30000,1000){
*
*公共无效onTick(长millisUntilFinished){
* mTextField.setText(秒时:+ millisUntilFinished / 1000);
*}
*
*公共无效onFinish(){
* mTextField.setText(完成了!);
*}
* }。开始();
*< / pre>
*
*在调用{@link #onTick(长)}同步到该物体,
*调用一次{@link #onTick(长)}永远不会发生之前的previous
*回调完成。这是只有在相关的实施
* {@link #onTick(长)}花费的时间量来执行是显著
*相比倒计时间隔。
* /
公共抽象类MyCountDownTimer {
/ **
*米利斯纪元以来报警时应该停止。
* /
专用长mMillisInFuture;
/ **
*用户收到的回调米利斯的间隔
* /
专用长mCountdownInterval;
专用长mStopTimeInFuture;
/ **
* @参数millisInFuture米利斯的数目在将来从呼叫
*以{@link #START()}直至倒计时完成,{@link #onFinish()}
*被调用。
*参数countDownInterval沿途的间隔接收
* {@link #onTick(长)}回调。
* /
公共MyCountDownTimer(长millisInFuture,长countDownInterval){
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}
公共无效setMillisInFuture(长millisInFuture){
this.mMillisInFuture = millisInFuture;
}
公共无效setCountdownInterval(长countdownInterval){
this.mCountdownInterval = countDownInterval;
}
/ **
*取消倒计时。
* /
公众最终无效取消(){
mHandler.removeMessages(MSG);
}
/ **
*开始倒计时。
* /
公共同步最后MyCountDownTimer的start(){
如果(mMillisInFuture&所述; = 0){
onFinish();
回到这一点;
}
mStopTimeInFuture = SystemClock.elapsedRealtime()+ mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
回到这一点;
}
/ **
*回调开枪固定的间隔。
* @参数millisUntilFinished的时间量,直到完成。
* /
公共抽象无效onTick(长millisUntilFinished);
/ **
*回调时激发的时间到了。
* /
公共抽象无效onFinish();
私有静态最终诠释味精= 1;
//手柄倒计时
私人处理程序mHandler =新的处理程序(){
@覆盖
公共无效的handleMessage(信息MSG){
同步(MyCountDownTimer.this){
最终长millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
如果(millisLeft&所述; = 0){
onFinish();
}否则如果(millisLeft< mCountdownInterval){
//没有打勾,只是推迟,直到做完
sendMessageDelayed(obtainMessage(MSG),millisLeft);
} 其他 {
长lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
//考虑到用户的onTick服用的时间来执行
长延时= lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
//特殊情况:用户onTick花了超过间隔
//完整,跳到下一个时间间隔
而(延迟℃下)延迟+ = mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG),延迟);
}
}
}
};
}
,然后你可以很容易地改变millisInFuture是这样的:
MyCountDownTimer remainingTimeCounter =新MyCountDownTimer(futureInMillis,1000){
公共无效onTick(长millisUntilFinished){
remainingTime = calculateRemainingTime(millisUntilFinished / 1000);
runOnUiThread(录入);
}
公共无效onFinish(){
// TODO:与millisInFuture = 4000(4秒)重新启动计数器
//取消(); //没有必要呼叫的取消这里()方法
this.setMillisInFuture(4000); //这里我们改变定时器的millisInFuture
this.start();
}
}。开始();
I want to restart countdown timer with new time when countdown timer finishes. I am giving code below:
futureInMillis = newTime();
CountDownTimer remainingTimeCounter = new CountDownTimer(futureInMillis, 1000) {
public void onTick(long millisUntilFinished) {
remainingTime = calculateRemainingTime(millisUntilFinished / 1000);
runOnUiThread(updateTime);
}
public void onFinish() {
// TODO: restart counter
cancel();
futureInMillis = newTime();
// remainingTimeCounter = null;
this.start();
}
}.start();
解决方案
we should know what the newTime()
do , and you don't have to call the method cancel()
, because the timer is in the onFinish()
method , that's means that the timer is finished counting down :
CountDownTimer remainingTimeCounter = new CountDownTimer(futureInMillis, 1000) {
public void onTick(long millisUntilFinished) {
remainingTime = calculateRemainingTime(millisUntilFinished / 1000);
runOnUiThread(updateTime);
}
public void onFinish() {
// TODO: restart counter
//cancel(); // there is no need the call the cancel() method here
futureInMillis = newTime();
// remainingTimeCounter = null;
this.start();
}
}.start();
EDIT :
there is no attribut in the class CountDownTimer
to use in order to change the futureInMillis ,
if you want to use that , you should use two CountDownTimer
; you should create two classes , each class has a timer and an attribut of futureInMillis , and then when the timer of the first Class finished, instanciate the second class , pass the futureInMillis to it , and start her timer, and vice versa .
**EDIT 2 :**
Or simply Edit the code source of the class CountDownTimer from the source code android and add setters and getters for attributs futureInMillis like this :
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//add your package declaration here
import android.util.Log;
/**
* Schedule a countdown until a time in the future, with
* regular notifications on intervals along the way.
*
* Example of showing a 30 second countdown in a text field:
*
* <pre class="prettyprint">
* new CountdownTimer(30000, 1000) {
*
* public void onTick(long millisUntilFinished) {
* mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
* }
*
* public void onFinish() {
* mTextField.setText("done!");
* }
* }.start();
* </pre>
*
* The calls to {@link #onTick(long)} are synchronized to this object so that
* one call to {@link #onTick(long)} won't ever occur before the previous
* callback is complete. This is only relevant when the implementation of
* {@link #onTick(long)} takes an amount of time to execute that is significant
* compared to the countdown interval.
*/
public abstract class MyCountDownTimer {
/**
* Millis since epoch when alarm should stop.
*/
private long mMillisInFuture;
/**
* The interval in millis that the user receives callbacks
*/
private long mCountdownInterval;
private long mStopTimeInFuture;
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}
public void setMillisInFuture(long millisInFuture) {
this.mMillisInFuture = millisInFuture;
}
public void setCountdownInterval(long countdownInterval) {
this.mCountdownInterval = countDownInterval;
}
/**
* Cancel the countdown.
*/
public final void cancel() {
mHandler.removeMessages(MSG);
}
/**
* Start the countdown.
*/
public synchronized final MyCountDownTimer start() {
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
/**
* Callback fired on regular interval.
* @param millisUntilFinished The amount of time until finished.
*/
public abstract void onTick(long millisUntilFinished);
/**
* Callback fired when the time is up.
*/
public abstract void onFinish();
private static final int MSG = 1;
// handles counting down
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (MyCountDownTimer.this) {
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
// special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}
and then you can change the millisInFuture easily like this :
MyCountDownTimer remainingTimeCounter = new MyCountDownTimer(futureInMillis, 1000) {
public void onTick(long millisUntilFinished) {
remainingTime = calculateRemainingTime(millisUntilFinished / 1000);
runOnUiThread(updateTime);
}
public void onFinish() {
// TODO: restart counter with millisInFuture = 4000 ( 4 seconds )
//cancel(); // there is no need the call the cancel() method here
this.setMillisInFuture(4000); // here we change the millisInFuture of our timer
this.start();
}
}.start();
相关推荐
最新文章