支付倒计时

来源:互联网 发布:jersey 返回对象json 编辑:程序博客网 时间:2024/05/16 11:41

在我们购物的时候会用到支付倒计时这个功能,其实Android已经帮封装好了一个类 CountDownTimer。

在一个TextView中不断显示剩下的时间,代码如下:

private TextView mTvSurplusTime;

private CountDownTimer timer = new CountDownTimer(20*60*1000,1000) {

@Override

public void onTick(long millisUntilFinished) {

long minute = millisUntilFinished/(60*1000);

long second=(millisUntilFinished-minute*60*1000)/1000;

String convertTime=minute+"分"+second+"秒";

mTvSurplusTime.setText("支付剩余时间"+convertTime);

}

@Override

public void onFinish() {

Util.showToast("20分钟已到,请重新下单",mContext);

finish();

}

};

调用的时候很简单:timer.start();

在CountDownTimer timer = new CountDownTimer(20*60*1000,1000)中,第一个参数表示总时间,第二个参数表示间隔时间。意思就是每隔一秒会回调一次方法onTick,然后20分钟之后会回调onFinish方法。

下面是CountDownTimer的源码,从源码可以发现,CountDownTimer也是基于Handler进行处理的:

packageandroid.os;

/**

* 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:

*

*

* new CountDownTimer(30000, 1000) {

*

*    public void onTick(long millisUntilFinished) {

*        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);

*    }

*

*    public void onFinish() {

*        mTextField.setText("done!");

*    }

*  }.start();

*

*

* 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 classCountDownTimer{

/**

* Millis since epoch when alarm should stop.

*/

private final longmMillisInFuture;

/**

* The interval in millis that the user receives callbacks

*/

private final longmCountdownInterval;

private longmStopTimeInFuture;

/**

* boolean representing if the timer was cancelled

*/

private booleanmCancelled=false;

/**

*@param millisInFutureThe number of millis in the future from the call

*  to {@link#start()} until the countdown is done and {@link#onFinish()}

*  is called.

*@param countDownIntervalThe interval along the way to receive

*  {@link#onTick(long)} callbacks.

*/

publicCountDownTimer(longmillisInFuture,longcountDownInterval) {

mMillisInFuture=millisInFuture;

mCountdownInterval=countDownInterval;

}

/**

* Cancel the countdown.

*/

public synchronized final voidcancel() {

mCancelled=true;

mHandler.removeMessages(MSG);

}

/**

* Start the countdown.

*/

public synchronized finalCountDownTimerstart() {

mCancelled=false;

if(mMillisInFuture<=0) {

onFinish();

return this;

}

mStopTimeInFuture=SystemClock.elapsedRealtime()+mMillisInFuture;

mHandler.sendMessage(mHandler.obtainMessage(MSG));

return this;

}

/**

* Callback fired on regular interval.

*@param millisUntilFinishedThe amount of time until finished.

*/

public abstract voidonTick(longmillisUntilFinished);

/**

* Callback fired when the time is up.

*/

public abstract voidonFinish();

private static final intMSG=1;

// handles counting down

privateHandlermHandler=newHandler() {

@Override

public voidhandleMessage(Messagemsg) {

synchronized(CountDownTimer.this) {

if(mCancelled) {

return;

}

final longmillisLeft=mStopTimeInFuture-SystemClock.elapsedRealtime();

if(millisLeft<=0) {

onFinish();

}else if(millisLeft

// no tick, just delay until done

sendMessageDelayed(obtainMessage(MSG),millisLeft);

}else{

longlastTickStart=SystemClock.elapsedRealtime();

onTick(millisLeft);

// take into account user's onTick taking time to execute

longdelay=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);

}

}

}

};

}

原创粉丝点击